Throwing a Second Exception Flashcards

1
Q

Give a counter intuitive characteristic about catch and finally statements !

A

A catch or finally block can contain any valid java code including another try statement

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

Give what problems you should be looking for when you see a
Method that tries to read a file in the OCA exam !

A

Even though the topic of reading files is on the OCP exam, the OCA exam may ask you about exception handling with those classes.

This is actually a gift. When you see such a question, you know the problem has to be about basic Java syntax or exception handling!

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

Give an example that illustrates all problems that you can face
with a method that tries to read a file !

A

16: public static void main(String[] args) {
17: FileReader reader = null;
18: try {
19: reader = read();
20: } catch (IOException e) {
21: try {
22: if (reader != null) reader.close();
23: } catch (IOException inner) {
24: }
25: }
26: }
27: private static FileReader read() throws IOException {
28: // CODE GOES HERE
29: }

  • The easiest case is if line 28 doesn’t throw an exception.
    Then the entire catch block on lines 20–25 is skipped.
  • Next, consider if line 28 throws a NullPointerException. That isn’t an IOException, so the catch block on lines 20–25 will still be skipped.
  • If line 28 does throw an IOException, the catch block on lines 20–25 does get run
  • Line 22 tries to close the reader. If that goes well, the code completes and the main() method ends normally.
  • If the exception thrown by close() is not an IOException: The catch block on line 23 does not handle it. Since there’s no other catch block for different exceptions, this exception propagates up and causes the main() method to terminate with that exception.
  • If the catch block on line 23 is empty: Even if the exception thrown by close() is an IOException, the empty catch block does not handle or process it. As a result, the exception still propagates up and causes the main() method to terminate with that exception.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give a tricky case where the exception from the catch block gets forgotten about !

A

26: try {
27: throw new RuntimeException();
28: } catch (RuntimeException e) {
29: throw new RuntimeException();
30: } finally {
31: throw new Exception();
32: }

  • Line 27 throws an exception, which is caught on line 28.
  • The catch block then throws an exception on line 29.
  • If there were no finally block, the exception from line 29 would be thrown
  • However, the finally block runs after the try block. Since the finally block throws an exception of its own on line 31, this one gets thrown. The exception from the catch block gets forgotten about
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example full of tricks and pitfalls !

A

it can be considered a good practice to use a try-catch block inside a finally block to handle or log exceptions that occur during the execution of the finally block. This practice helps prevent exceptions thrown in the finally block from masking or hiding exceptions thrown in the try or catch blocks.

The try-catch inside the finally block ensures that any exceptions thrown during the finalization phase are caught and handled, preventing them from interfering with or masking the exceptions thrown in the try or catch blocks.

While it is a good practice to use try-catch inside the finally block for complex scenarios, it may not always be necessary. For simple cases where the finally block does not perform operations that might throw exceptions, it might be sufficient to handle exceptions in the try and catch blocks alone.

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