学院首页>网络编程>JSP>Java编程思想读书笔记(10章上)

Java编程思想读书笔记(10章上)

作者: 来源: 添加时间:2006-5-24 13:03:35

   3. 取得异常中的信息的几个函数

   1) String getMessage()、getLocalizedMessage 、toString

   取得异常对象中的信息
   import java.lang.RuntimeException;
   import java.lang.NullPointerException;
   Import java.sql.SQLException;
   import java.io.IOException;
   class TestException{
   public void tSql() throws SQLException {
   System.out.println("Originating the exception in tSql()");
   throw new SQLException("throw in tSql");
   }
   }
   public class Test{
   public static void main(String[] args){
   TestException te = new TestException();
   try{
   te.tSql();
   }
   catch(SQLException ex){
   System.out.println("catch SQLException in main");
   System.out.println("ex.getMessage():" + ex.getMessage());
   System.out.println("ex.getLocalizedMessage():" +
   ex.getLocalizedMessage());
   System.out.println("ex.toString():" + ex.toString());
   }
   catch(Exception ex){
   System.out.println("catch Exception in main");
   }
   }
   }
   运行结果:

   Originating the exception in tSql()

   catch SQLException in main
   ex.getMessage():throw in tSql
   ex.getLocalizedMessage():throw in tSql
   ex.toString():java.sql.SQLException: throw in tSql
   2) void printStackTrace()、Throwable fillStackTrace()

   printStackTrace打印出Throwable和其callstacktrace。

   FillStackTrace则在调用点设立新的stacktrace信息
   import java.sql.SQLException;
   class TestException{
   public static void tSql() throws SQLException {
   System.out.println("Originating the exception in tSql()");
   throw new SQLException("throw in tSql");
   }
   public void f() throws SQLException{
   try{
   tSql();
   }
   catch(SQLException ex){
   System.out.println("In f(), e.printStackTrace()");
   ex.printStackTrace();
   throw ex;//(1)

   //throw (SQLException)ex.fillInStackTrace();(2)

   }
   }
   }
   public class Test{
   public static void main(String[] args){
   TestException te = new TestException();
   try{
   te.f();
   }
   catch(SQLException ex){
   System.out.println("catch in main, e.printStackTrace()");
   ex.printStackTrace();
   }
   catch(Exception ex){
   System.out.println("catch Exception in main");
   }
   }
   }
   结果为:

   Originating the exception in tSql()

   In f(), e.printStackTrace()

   catch in main, e.printStackTrace()

   java.sql.SQLException: throw in tSql
   void TestException.tSql()

   Test.java:5
   void TestException.f()

   Test.java:9
   void Test.main(java.lang.String[])

   Test.java:22
   java.sql.SQLException: throw in tSql
   void TestException.tSql()

   Test.java:5
   void TestException.f()

   Test.java:9
   void Test.main(java.lang.String[])

   Test.java:22
   如果把(1)处代码注释掉,并去年(2)处代码的注释,结果将变成:

   Originating the exception in tSql()

   In f(), e.printStackTrace()

   catch in main, e.printStackTrace()

   java.sql.SQLException: throw in tSql
   void TestException.tSql() //(3)

   Test.java:6
   void TestException.f()

   Test.java:10
   void Test.main(java.lang.String[])

   Test.java:24
   java.sql.SQLException: throw in tSql
   void TestException.f()//(4)

   Test.java:16
   void Test.main(java.lang.String[])

   Test.java:24
   由于在代码(2)处设立新的stacktrace信息,所以异常会被认为是在f()中发出的,所以在main()中得到的异常原始抛出点为f()(见(3)),而在f()中为tSql()(见(6))。

   3) 如果重新抛出一个不同类型的异常,也能产生fillStackTrace()函数的效果。如果把上面代码的f()函数修改成下面的样子:

   public void f() throws SQLException,IOException{
   try{
   tSql();
   }
   catch(SQLException ex){
   System.out.println("In f(), e.printStackTrace()");
   ex.printStackTrace();
   throw new IOException(); //(1)

   }
   }
   则结果为:

   Originating the exception in tSql()

   In f(), e.printStackTrace()

   catch Exception in main
   java.sql.SQLException: throw in tSql
   void TestException.tSql()

   Test.java:6
   void TestException.f()

   Test.java:10
   void Test.main(java.lang.String[])

   Test.java:25
   java.io.IOException
   void TestException.f()

   Test.java:17
   void Test.main(java.lang.String[])

   Test.java:25
   由于在(1)处抛出了一个新的类型的异常,那么在main()中捕捉到的是新的异常,所以在main()中捕捉到的异常的原始抛出点为f()。

   4. RuntimeException异常

   RuntimeException及其子类所代表的异常我们在程序中不用进行捕捉,如果发生此类异常,Java会自动抛出相应的异常对象,如:

   class TestException{
   public static void g(int x) {
   System.out.println("10/" + x + " = " + 10/x);
   }
   }
   public class Test{
   public static void main(String[] args){
   TestException.g(2);
   TestException.g(0);//(1)

   }
   }
   上面代码在编译时不会发生错误,只有在运行时(1)处会发生错误。虽然除法可能会存在错误,但我们不用进行捕捉,当发生错误时,Java会自动抛出相应异常。
第 2 页,共 2 页 [1] [2]
站内搜索