Somehow i came across a bug today and i was trying to replicate it.
I believe the below code will throw a NullPointerException because ep is not instantiated, but i am unable to simulate it..
I believe that the instance variable ep is null when the exception is thrown from the main. But running it doesn't show NullPointerException.
When the exception is thrown (throw e.getILException(100)), is the MyException.MyException() constructor called?
package sg.java.testException;
public class MyException extends Exception {
Exception ep;
public MyException() {
super();
ep = new Exception();
}
public OtherException getILException(long txnId) {
// the error is here, according to the stacktrace
return new OtherException(ep, ep.toString());
}
}
Caller:
public static void main(String args[]) throws OtherException{
try{
Car.run();
}catch (MyException e){
throw e.getILException(100);
}
}
Car Codes:
public class Car {
public static void run() throws MyException {
throw new MyException();
}
}
I want to be able to replicate this exception:
java.lang.NullPointerException
at sg.java.testException.MyException.getILException(MyException.java:13)
at sg.java.testException.ExceptionTest.main(ExceptionTest.java:8)
Thanks!