Catching Various Types of Exceptions Flashcards
Give the 2 things that you need to know about exceptions in the
OCA exam !
The OCA exam can define basic exceptions to show you the hierarchy. You only need to do two things with this information :
First, you must be able to recognize if the exception is a checked or an unchecked exception.
Second, you need to determine if any of the exceptions are subclasses of the others.
Give examples for each case !
In this example, there are three custom exceptions. All are unchecked exceptions because they directly or indirectly extend RuntimeException.
- class AnimalsOutForAWalk extends RuntimeException { }
- class ExhibitClosed extends RuntimeException { }
- class ExhibitClosedForLunch extends ExhibitClosed { }
In the example above, ExhibitClosedForLunch is a subclass of ExhibitClosed
Give an example for catching 2 exceptions thrown by the same
method !
public void visitPorcupine() {
try {
seeAnimal();
} catch (AnimalsOutForAWalk e) {// first catch block
System.out.print(“try back later”);
} catch (ExhibitClosed e) {// second catch block
System.out.print(“not today”);
}
}
There are three possibilities for when this code is run.
If seeAnimal() doesn’t throw an exception, nothing is printed out.
If the animal is out for a walk, only the first catch block runs.
If the exhibit is closed, only the second catch block runs.
Give the rule about the catch blocks !
The compiler looks at the catch block in the order they appear in
the file. If a catch blocks which catch a given exception and the
next catch block catches the subclass of that given exception, it will result in a compilation error
Give an example where subsequent catch blocks contain an exception and its subclass !
class TEST{
static void super_method(){
throw new SuperClass();
}
static void sub_method(){
throw new SubClass();
}
private static void eatCarrot() { try { sub_method(); } catch(SuperClass sr){ System.out.println("SuperClass"); } catch(SubClass sb){ System.out.println("SubClass"); }}
public static void main(String[] args) {
eatCarrot();
}
} class SuperClass extends RuntimeException {} class SubClass extends SuperClass {}
Give an example of unreachable catch block (code) !
class TEST{
static void super_method(){
throw new SuperClass();
}
static void sub_method(){
throw new SubClass();
}
private static void eatCarrot() { try { sub_method(); } catch(SuperClass sr){ System.out.println("SuperClass"); } catch(SubClass sb){ System.out.println("SubClass"); }}
public static void main(String[] args) {
eatCarrot();
}
} class SuperClass extends RuntimeException {} class SubClass extends SuperClass {}
The second catch block in the example above is considered unreachable
Give a general rule about catching multiple exceptions !
In Java, when handling exceptions, the order of catch blocks is crucial because it determines how exceptions are caught. More specific exceptions (subclasses) should be caught before more general exceptions (superclasses) to avoid unreachable code issues.