finallydemo.java

来自「Java学习源代码检索系统免费版」· Java 代码 · 共 48 行

JAVA
48
字号
//==============================================================
// FinallyDemo.java - Demonstrate try-catch-finally statements
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

// Exception class
class ThrowMe extends Exception {
 ThrowMe() { }
 ThrowMe(String s) {
  super(s); 
 }
}

class FinallyDemo {
 // Test method -- pass 0, 1, or 2 for different exceptions
 static void testMethod(int n) throws Exception, ThrowMe {
  switch (n) {
  case 1: 
   throw new Exception("Unhandled exception");
  case 2:
   throw new ThrowMe("To the wolves");
  default:
   return;
  }
 }
 // Main program 
 public static void main(String args[]) 
  throws Exception {
  int argument = 0;
  if (args.length > 0)
   argument = Integer.parseInt(args[0]);
  try {
   testMethod(argument);
  } catch (ThrowMe e) {
   System.out.println("ThrowMe: " + e.getMessage());
  } finally {
   System.out.println("Finally statement");
  }
  System.out.println("Statement after try block");
 }
}

⌨️ 快捷键说明

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