finallyexam.java

来自「Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程」· Java 代码 · 共 41 行

JAVA
41
字号
public class finallyExam {
	private static int iStart = 0;
	private static int iEnd = 50;
  public static void main(String args[]) {
    testFinally();
  }
  public static void throwException() throws Exception {
    try {
      System.out.println("Method throwException");
      throw new Exception(); // generate exception
    }
    catch (Exception exception) {
      System.err.println("Exception handled in method throwException");
      throw exception; // rethrow for further processing
    }
    finally {
      System.err.println("Finally executed in throwException");
    }
  }
  public static void testFinally() {
    try {
      while(true){
      	if(iStart == iEnd)
      		throwException();
      	iStart ++;
      	iEnd --;
      }
    }
    catch (Exception exception) {
    	System.out.println("iStart = iEnd, throw Exception");
      System.err.println(exception.toString());
    }
    finally {
      System.out.println("In finally: 重新初始化 iStart and iEnd");
      iStart = 0;
      iEnd = 50;
    }
    System.out.println("End of method testFinally");
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?