Assume InvalidResourceException is a subclass of ResourceException. Define two methods:
void handleException(ResourceException e) {
System.out.println("ResourceException " + e.toString());
}
void handleException(InvalidResourceException e) {
System.out.println("InvalidResourceException " + e.toString());
}
Now the following code:
try {
throw new InvalidResourceException("invalid resource");
} catch (ResourceException e) {
handleException(e);
}
prints this:
ResourceException: com.myPackage.InvalidResourceException: invalid resource
But the following code:
try {
throw new InvalidResourceException("invalid resource");
} catch (InvalidResourceException e) {
handleException(e);
} catch (ResourceException e) {
handleException(e);
}
prints this:
InvalidResourceException: com.myPackage.InvalidResourceException: invalid resource
This is with Sun's JDK 1.5.0_15.
Is this consistent with the Java standard?
What should this code do?
Exception e = new InvalidResourceException("invalid resource");
handleException(e);
What should this code do?
Exception e = new InvalidResourceException("invalid resource");
if (e instanceOf ResourceException) {
handleException(e);
} else if (e instanceOf InvalidResourceException) {
handleException(e);
} else {
handleException(e):
}