Thinking in Java 3 Edition 此外你还可以用 Throwable的基类,也就是 Object(所有对象的基类) 的方法。对异常来说有一个很有用的方法, getclass()。它会返回一个 “表示这个对象是属于哪种类型的” class对象。接下来,你可以用 getName()查询这个 Class对象的名字。你还可以用 Class对象作 更复杂的事情,不过对异常处理来说,就没这个必要了 下面这段程序演示了这些基本的 Exception方法的用法 //: c09: ExceptionMethods java / Demonstrating the Exception Methods import com. bruceeckel simpletest*i public class ExceptionMethods t private static Test monitor new Test() public static void main(String[] args) i ception("My Exe 1 catch(Exception e)t println (" Caught e getMessage() e get LocalizedMessage()ig System. err println("getLocali mEssage(): stem.err println("print stackTrace():")i e print stackTrace()i String[] t Caught Exception getMessage () My Exception" tostring () java. lang Except My Exception "printstackTrace(): 号号\ tat ExceptionMethods,main\(.*\)” 你能看到这些方法一个比一个提供更多的信息—实际上它们每个都是前 个的超集 重抛异常 有时你需要重新抛出那个刚捕捉到的,用 Exception捕捉到的异常。由 于你已经有了当前异常的 reference,因此你可以直接将那个 reference 重抛出来 System. err println("An exception was thrown") 第11页共11页 www.wgqqh.com/shhgs/tij.html
Thinking in Java 3 rd Edition ✞ 11 ✟ ✠ 11 ✟ www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com ±³S Th rowable #$ Object(,-÷`) @n÷èZ`a-^\ä-@n#getCl ass( )9~op^\ øùR\÷`jkã?ÐCl ass ÷`|`#±S getN am e( )¨1R\ Cl ass ÷`±³S Cl ass ÷`» ói2¢#=÷èZ78`a# ÀR\; R¨%&3ù R2 Exception @nnÊ //: c09:ExceptionMethods.java // Demonstrating the Exception Methods. import com.bruceeckel.simpletest.*; public class ExceptionMethods { private static Test monitor = new Test(); public static void main(String[] args) { try { throw new Exception("My Exception"); } catch(Exception e) { System.err.println("Caught Exception"); System.err.println("getMessage():" + e.getMessage()); System.err.println("getLocalizedMessage():" + e.getLocalizedMessage()); System.err.println("toString():" + e); System.err.println("printStackTrace():"); e.printStackTrace(); } monitor.expect(new String[] { "Caught Exception", "getMessage():My Exception", "getLocalizedMessage():My Exception", "toString():java.lang.Exception: My Exception", "printStackTrace():", "java.lang.Exception: My Exception", "%% \tat ExceptionMethods.main\\(.*\\)" }); } } ///:~ ±'R2@n^\^\;ó[GHãg9:¸\.* ^\4 -±;ÔLK\5# Exception èZ k±ËÌ- F*èZ referen ce#ƱSÇ|DK\ referen ce L`Ê catch(Exception e) { System.err.println("An exception was thrown"); throw e; }
hapter 9: Error Handl 重抛会把异常送到更高一层的异常处理程序去。同一个try区块的其它 catch子句都将被忽略。此外,它还会保留异常对象里的所有信息,这 样捕获这个异常的上一层的异常处理程序就能够提取这个对象中的所有信 息了 如果你直接重抛当前的异常,则 printstackTrace()所打印出来的那 些保存在异常对象里的信息,还会指向异常发生的地方,它们不会被指到 你重抛异常的地点。如果你要装载新的栈轨迹信息,你可以调用 fillIn stackTrace(),这个方法会将当前栈的信息塞进旧的异常对象 中,并返回一个 Throwable对象。下面就是它的工作方式 //: c09: Rethrowing. java // Demonstrating fillInStackTrace() import com. bruceeckel. simpletest * i public class Rethrowing i Test oi System. out. println ("originating the exception in f()") throw new Exception("thrown from f()")i public static void g() throws Throwable t f(); h(Exception e)t System. err println ("Inside g(,e. print stackTrace()) e print stackTrace()i / thr 1lInstackT public static void main(String[] args)throws Throwable t g(); 1 catch(Exception e)( System. err. println( Caught in main, e printStackTrace()")i e print stackTrace() monitor. expect (new String[] i originating the exception in f()", () java. lang Exception: thrown from f()", 号号\ tat Rethrowing.f(,*?)”, 号号\ tat Throwing.q(.*?)”, 号号\ tat Rethrowing,main(.*?) "Caught in main, e. print stackTrace()", thrown from f() 号号\ tat Rethrowing.f(.*?)", 号号\ tat Throwing,g(,*?) 号号\ tat Throwing,main(.*?) 第12页共12页 www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com
Chapter 9: Error Handling with Exceptions www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com ✞ 12 ✟ ✠ 12 ✟ L~èZöór^KèZ78%&´^\ try B«U9 catch N¹.D/67#9³~8èZ÷`,-GH#R OR\èZg^KèZ78%& 'GR\÷`!,-G H ¶·±Ç|LF*èZ#ï printStackTrace( ),ñò`K 2xèZ÷`GH#³~Å÷èZ0ü@#9:~/Å ±LèZ¯¶·±;9Ô()GH#±Sº fil l InStackTrace( )#R\@n~DF*GH:±;èZ÷` !#iop^\ Th rowable ÷` 9<»@AÊ //: c09:Rethrowing.java // Demonstrating fillInStackTrace() import com.bruceeckel.simpletest.*; public class Rethrowing { private static Test monitor = new Test(); public static void f() throws Exception { System.out.println("originating the exception in f()"); throw new Exception("thrown from f()"); } public static void g() throws Throwable { try { f(); } catch(Exception e) { System.err.println("Inside g(),e.printStackTrace()"); e.printStackTrace(); throw e; // 17 // throw e.fillInStackTrace(); // 18 } } public static void main(String[] args) throws Throwable { try { g(); } catch(Exception e) { System.err.println( "Caught in main, e.printStackTrace()"); e.printStackTrace(); } monitor.expect(new String[] { "originating the exception in f()", "Inside g(),e.printStackTrace()", "java.lang.Exception: thrown from f()", "%% \tat Rethrowing.f(.*?)", "%% \tat Rethrowing.g(.*?)", "%% \tat Rethrowing.main(.*?)", "Caught in main, e.printStackTrace()", "java.lang.Exception: thrown from f()", "%% \tat Rethrowing.f(.*?)", "%% \tat Rethrowing.g(.*?)", "%% \tat Rethrowing.main(.*?)"