标签:
package chapterone.zidingyiyichang; /** * 此程序包含了关于异常的综合性知识; * 执行的顺序如标号所示 */ import java.io.*; import java.sql.*; public class TestException { public static void main(String args[]) { try { System.out.println("main1");// 1.打印main1 ma();//2.调用ma()方法; 9.最后方法ma()抛出一个IOException异常,需要捕获 System.out.println("main2"); } catch (Exception e) { System.out.println("Catch Exception in main");// 10.捕获方法ma()的IOException异常,打印Catch Exception in main" System.out.println(e.getMessage());//11.然后再打印e.getMessage(),也就是sql exception in mb } } public static void ma() throws IOException { try { System.out.println("ma1");//3.打印ma1; mb();//4.调用mb()方法; 6.方法mb()抛出的SQLException的异常,需要捕获异常 System.out.println("ma2"); } catch (SQLException e) { System.out.println("Catch SQLException in ma");// 7.捕获方法mb()抛出的SQLException的异常,打印Catch SQLException in ma; // exception in mb" throw new IOException(e.getMessage()); //8.又抛出一个IOException异常,要输出e.getMessage();需要进行捕获。 } catch (Exception e) { System.out.println("Catch Exception in ma"); System.out.println(e.getMessage()); } } public static void mb() throws SQLException { throw new SQLException("sql exception in mb");
//5.抛出SQLException的异常,具体异常详细是打印sql exception in mb。即e.getMessage()的信息是“sql exception in mb”; } /** * 结果为: * main1 * ma1 * Catch SQLException in ma * Catch Exception in main * sql exception in mb */ }
标签:
原文地址:http://www.cnblogs.com/vitelon/p/4888134.html