Java-10.6 finally (1)-Features
In this chapter, we will discuss finally.
Feature: finally can always be executed.
package com.ray.ch10;public class Test {private void test() throws Exception {throw new Exception();}public static void main(String[] args) {try {new Test().test();} catch (Exception e) {e.printStackTrace();} finally {System.out.println(finally);}}}
Output:
Java. lang. Exception
At com. ray. ch10.Test. test (Test. java: 6)
At com. ray. ch10.Test. main (Test. java: 11)
Finally
Another example:
package com.ray.ch10;public class Test {public static void main(String[] args) {int i = 0;while (true) {try {if (i++ == 0) {throw new Exception();}} catch (Exception e) {e.printStackTrace();} finally {System.out.println(finally);if (i == 2) {System.out.println(break);break;}}}}}
Output:
Java. lang. Exception
At com. ray. ch10.Test. main (Test. java: 10)
Finally
Finally
Break
Summary: This chapter describes the finally features.
This chapter is here. Thank you.