#243 Problem throwing an exception

open
nobody
None
5
2005-08-25
2005-08-25
Nigel
No

> cat t97.java
class t97 {
static void aProc() {
System.out.println("Exceptional Procedure");
NullPointerException e = new NullPointerException
("Nigel");
e.initCause(new ArithmeticException("of course"));
throw e;
}

public static void main(String[] arg) {
try {
aProc();
} catch (NullPointerException e) {
System.out.println("Who is best: " + e.getMessage
());
System.out.println("Are you sure?: " + e.getCause
().getMessage());
}
}
}

Process cat exited with code 0

Java produces the following:

> javac t97.java
Process javac exited with code 0
> java t97
Exceptional Procedure
Who is best: Nigel
Are you sure?: of course
Process java exited with code 0

but BeanShell generates an exception:

> java -classpath /opt/bsh-2.0b5.jar:. bsh.Interpreter
t97.java
Exceptional Procedure
Class: class t97 main method threw
exception:Exception invoking imported object method. :
at Line: 12 : in file: t97.java : aProc ( )

Target exception:
java.lang.reflect.InvocationTargetException

Process java exited with code 0

Discussion