2015-09-07 11:44:36 -06:00
|
|
|
// exceptions/ExtraFeatures.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Further embellishment of exception classes
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
class MyException2 extends Exception {
|
|
|
|
private int x;
|
2017-05-01 14:33:10 -06:00
|
|
|
MyException2() {}
|
|
|
|
MyException2(String msg) { super(msg); }
|
|
|
|
MyException2(String msg, int x) {
|
2015-06-15 17:47:35 -07:00
|
|
|
super(msg);
|
|
|
|
this.x = x;
|
|
|
|
}
|
|
|
|
public int val() { return x; }
|
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
2016-01-25 18:05:55 -08:00
|
|
|
return "Detail Message: "+ x
|
|
|
|
+ " "+ super.getMessage();
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ExtraFeatures {
|
|
|
|
public static void f() throws MyException2 {
|
2017-05-10 11:45:39 -06:00
|
|
|
System.out.println(
|
|
|
|
"Throwing MyException2 from f()");
|
2015-06-15 17:47:35 -07:00
|
|
|
throw new MyException2();
|
|
|
|
}
|
|
|
|
public static void g() throws MyException2 {
|
2017-05-10 11:45:39 -06:00
|
|
|
System.out.println(
|
|
|
|
"Throwing MyException2 from g()");
|
2015-06-15 17:47:35 -07:00
|
|
|
throw new MyException2("Originated in g()");
|
|
|
|
}
|
|
|
|
public static void h() throws MyException2 {
|
2017-05-10 11:45:39 -06:00
|
|
|
System.out.println(
|
|
|
|
"Throwing MyException2 from h()");
|
2015-06-15 17:47:35 -07:00
|
|
|
throw new MyException2("Originated in h()", 47);
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
try {
|
|
|
|
f();
|
|
|
|
} catch(MyException2 e) {
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
g();
|
|
|
|
} catch(MyException2 e) {
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
h();
|
|
|
|
} catch(MyException2 e) {
|
|
|
|
e.printStackTrace(System.out);
|
|
|
|
System.out.println("e.val() = " + e.val());
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Throwing MyException2 from f()
|
|
|
|
MyException2: Detail Message: 0 null
|
2017-05-10 11:45:39 -06:00
|
|
|
at ExtraFeatures.f(ExtraFeatures.java:24)
|
|
|
|
at ExtraFeatures.main(ExtraFeatures.java:38)
|
2015-06-15 17:47:35 -07:00
|
|
|
Throwing MyException2 from g()
|
|
|
|
MyException2: Detail Message: 0 Originated in g()
|
2017-05-10 11:45:39 -06:00
|
|
|
at ExtraFeatures.g(ExtraFeatures.java:29)
|
|
|
|
at ExtraFeatures.main(ExtraFeatures.java:43)
|
2015-06-15 17:47:35 -07:00
|
|
|
Throwing MyException2 from h()
|
|
|
|
MyException2: Detail Message: 47 Originated in h()
|
2017-05-10 11:45:39 -06:00
|
|
|
at ExtraFeatures.h(ExtraFeatures.java:34)
|
|
|
|
at ExtraFeatures.main(ExtraFeatures.java:48)
|
2015-06-15 17:47:35 -07:00
|
|
|
e.val() = 47
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|