- package com.heima.exception;
- public class Demo_Finally {
- /**
- * try...catch...finally 执行流程测试
- */
- public static void main(String[] args) {
- method(1);
- method(2);
- function(1);
- function(2);
- System.out.println(s);
- }
-
- public static String s = "";
-
- public static void method(int i) {
- try {
- if(i == 1) {
- throw new Exception();
- }
- s += "1";
- } catch (Exception e) {
- s += "2";
- return;
- } finally {
- s += "3";
- }
- s += "4";
- }
-
- public static String function(int i) {
- try {
- if(i == 1) {
- throw new Exception();
- }
- s += "1";
- return s;
- } catch (Exception e) {
- s += "2";
- return s;
- } finally {
- s += "3";
- }
- }
-
- }
复制代码
|
|