Please examine my custom exception below:
public class ReportException extends Exception {
private int mCode = -1;
private String mString = "";
public ReportException(int code, String description)
{
super(description);
mCode = code;
mString = description;
}
public int getCode()
{
return mCode;
}
public String getString()
{
return mString;
}
}
My question is why is this illegal in another class:
try{
throw new NullPointerException();
}
catch(ReportException e){
}
To me a NullPointerException is derived from Exception class and so is my custom ReportException so since there the same type i'd expect it can be caught in the catch clause. But my IDE says this is illegal. I had this discussion with a colleague of mine and he said there it cant be done but im just wondering why since they both derive from the same Exception class. This looks to defy polymorphism.