Catching Various Types of Exceptions Flashcards

1
Q

Give the 2 things that you need to know about exceptions in the
OCA exam !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give examples for each case !

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example for catching 2 exceptions thrown by the same
method !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give the rule about the catch blocks !

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example where subsequent catch blocks contain an exception and its subclass !

A

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 {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give an example of unreachable catch block (code) !

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give a general rule about catching multiple exceptions !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly