Im trying to use the instanceof operator within the context of error handling but the code seems to be not working properly.
I just want to specify one superclass and be able to determine which subclass is being caught. No casting is necessary since i dont need to work with the object.
Thanks for any help. :)
Code:// no casting necessary
try{
.........
}catch(Exception e){
if(e instanceof FileNotFoundException){
FileNotFoundException fnf = (FileNotFoundException) e;
fnf.getMessage();
}
}
Code:// what i would like to do
try{
............
}catch(Exception e){
if(e instanceof FileNotFoundException){
System.out.println("FileNotFoundException!!!");
}
if(e instanceof NullPointerException){
System.out.println("NullPointerException!!!");
}
}
