正在加载图片...
Thinking in Java 3 Edition public static void main(String[] args)t F t catch(MyException e) e print stackTrace() ¥y g() 1 catch(MyExcept monitor. expect(new String[] i Throwing MyException from f()", "MYException", 号号\ tat Fu1 Constructors.f\\(.*\\) 号号\ tat Fu11 Constructors.main(.*\)", "Throwing MyException from g()", My Exception: Originated in g()", 号号\ tat Fu11 Constructors,g\\(.*1)", 号号\ tat Fu11 Constructors.main\(.*\\) }); }///: 新增的代码很少: MyException有两个构造函数。第二个通过 super 关键词,明确地用一个 String参数调用了基类的构造函数 处理程序调用了 Throwable类( Exception是从它那里继承的)的 printstackTrace()方法。这个方法会返回“被调用的方法是经过怎 样一个顺序到达异常发生地点”的信息。缺省情况下,这些信息会送到标 准错误流,但是这个方法的重载版也允许你将结果送到其它流 创建自定义的异常还能更进一步。你还可以添进额外的构造函数和成员: //: c09: ExtraFeatures java // Further embellishment of exception clas import com. bruceeckel. simpletest *i private int x public My ption()i] public MyException2(String msg) public My Exception2 (String msg, int x)i public string getMessage()t return "Detail Message: +x+ 第7页共7页 www.wgqqh.com/shhgs/tij.htmlThinking in Java 3 rd Edition ✞ 7 ✟ ✠ 7 ✟ www.wgqqh.com/shhgs/tij.html email:shhgs@sohu.com } public static void main(String[] args) { try { f(); } catch(MyException e) { e.printStackTrace(); } try { g(); } catch(MyException e) { e.printStackTrace(); } monitor.expect(new String[] { "Throwing MyException from f()", "MyException", "%% \tat FullConstructors.f\\(.*\\)", "%% \tat FullConstructors.main\\(.*\\)", "Throwing MyException from g()", "MyException: Originated in g()", "%% \tat FullConstructors.g\\(.*\\)", "%% \tat FullConstructors.main\\(.*\\)" }); } } ///:~ Ôû ä$ÊM yException -z\stuQ{}\<= su per „ˆõ#üQ^\ String ‚Qº…stuQ 78%&º… Th rowable (Exception b9K?É) printStackTrace( )@nR\@n~op/º@nÌ=N O^\[&ýèZ0ü¯GHþ# «#R2GH~öv yõ#+R\@n$U±D·öU9õ Îb•e:èZ³'ó±^±³žS±šstuQ³Í’Ê //: c09:ExtraFeatures.java // Further embellishment of exception classes. import com.bruceeckel.simpletest.*; class MyException2 extends Exception { private int x; public MyException2() {} public MyException2(String msg) { super(msg); } public MyException2(String msg, int x) { super(msg); this.x = x; } public int val() { return x; } public String getMessage() { return "Detail Message: "+ x + " "+ super.getMessage(); } }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有