Chapter 15 - Exception Handling Flashcards

1
Q

What can some API methods lead to because they are dangerous?

A

A runtime error

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

What is a technique for handling runtime errors?

A

Use exception handling. More specifically, surround the method call with a try block and insert a catch block immediately after the try block.

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

Try/Catch sytax and code example

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

What does NumberFormatException tell a user?

A

It’s thrown when a number’s format is inappropriate.

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

What causes the NumberFormatException to be thrown?

A

It’s thrown my one of the parse methods (Integer.parseInt, Long.parseLong, Double.parseDouble etc.)

When there’s an attempt to convert a string to a number and the string’s character don’t form a valid number.

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

How large can try blocks be?

A

Big or small, depends on the need.

It is legal to surround an entire method body with a try block, but that’s usually counterproductive because it makes it harder to identify the “dangerous” code.

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

What should you do if you have a chunk of code that has several “dangerous” method/constructor calls?

A
  • Add a seperate try-catch structure for each scuh call might result in cluttered code
  • To prevent this and improve program readability, consider using a single try block that surrounds the calls.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the JVM do if an exception is thrown?

A

It immediately jumps out of the current try block and looks for a matching catch block. The immediacy of the jump means that if there are statements in the try block after the exception-throwing statement, those statements are skipped.

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

What does the compiler assume in regards to a try block?

A

It knows that statements inside a try block might be skipped, so in turn it assumes that all statements inside a try block get skipped.

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

What can happen if an assignments is within a try block, but the same assignment doesn’t exist outside of the try block?

A

It can give you the following compilation error:

variable x might not have been initialized

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

How do you fix the variable x might not have been initialized error?

A

Initialize the variable prior to the try block.

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

What are the two types of exceptions?

A

Checked and Unchecked.

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

What is a check exception?

A

Checked exceptions are required to be checked with a try-catch mechanism.

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

What is an unchecked exception?

A

Unchecked exceptions are not required to be checked with a try-catch mechanism (but, as an option, unchecked exceptions may be checked with a try-catch mechanism)

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

How can you tell if a particular exception is classified as checked or unchecked?

A
  • Look up its associated class in the API documentation
  • On the class’s API page, look at its class hierarchy tree. If you find that the class is derived from the RuntimeException class or from the Error exceptions class, then it’s an unchecked exception. Otherwise, it’s a check exception.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Class hierarchy for Exception Classes visual

A
17
Q

What happens if an unchecked exception is thrown and not caught with a try-catch mechanism?

A

The program will crash

18
Q

How do you handle code that might thrown an unchecked exception?

A
  • Use a try-catch mechanism

or

  • Don’t attempt the catch the exception, but write the code carefully so as to avoid the possibility of the exception being thrown.
19
Q

Where should you look if you want to use a method or constructor from one of the API classes, but you’re unsure about it?

A

The API codumentation.

20
Q

What can the API documentation be used for?

A

- To determine if the method/constructor call could possible throw an exception (On the API documentation page for the method/constructor, look for a throws sections. If there’s a throws section, that means the method/constructor can possible throw an exception.)

- To determine whether the method/constructor call throw is a checked or unchecked exception.

21
Q

What must you add if a method/constructor can possible throw a checked exception?

A

A try-catch mechanism

22
Q

What do you do if a method/constructor can possible throw an unchecked exception?

A

Read teh API documentation to figure out the nature of the exception and then either:

  • Use a try-catch mechanism
  • Use careful code so that the exception won’t be thrown.
23
Q
A
24
Q

What should you do if several statements within a try block can possible throw an exception and the exceptions are of different types?

A
  • Provide a generic catch block that handles every type of exception that might be thrown.

or

  • Provide a sequence of specific catch blocks, with each catch block handling a different exception type or group of exception types.
25
Q

How do you define a catch block?

A

With an Exception parameter, e.

Example:

26
Q

In a catch block heading, what happens if you use an Exception parameter?

A

All thrown exceptions will match up with the exception parameter.

27
Q

What is required for a thrown exception to be caught by the catch block?

A

The throw exception’s class equals the catch heading’s parameter’s class or the thrown exception’s class is a subclass of the catch heading’s parameter’s class.

28
Q

What happens since every thrown exception’s class is a subclass of the Exception class?

A

All thrown exceptions will match up with a generic Exception parameter.

29
Q

What should you do when a try block might throw more than one type of exception?

A

Instead of providing one generic catch block, you can provide a sequence of catch blocks, with each catch block handling a different exception type or group of exception types.

30
Q

What happens if multiple catch blocks are used and an exception is thrown?

A

The first catch block that matches the type of exception thrown is the one excecuted and the other catch blocks are then skipped.

31
Q

What should you do if one of the catch block’s exception class is derived from another catch block’s exception class?

A

To avoid a compilation error, you must arrange the catch blocks with the more general exception classes at the bottom (the superclasses go at the bottom)

32
Q

What does the JVM do as part of a runtime error?

A

The JVM prints the exception that was thrown and then prints the call-stack trace.

The call-stack trace shows the methods that were call prior to the crash.

33
Q

When should you move the try and catch blocks from the called method back to the calling method?

A

If you have a utility method that might throw an exception, but the original causes of that exception are in the calling methods.

or

You have a non-void return method that sometimes throws an exception and that method’s exception handler cannot know what value to have the method return.

34
Q

How do you move the try and catch blocks from the called method back to the calling method?

A

Append throws “exception-type” to the called method’s header.

Example:

35
Q

What do you have to provide sometimes that executes regardless of whether an exception is thrown?

A

Cleanup code.

For example: After writing a file, you must close the files to complete the writing process. Closing also releases system resources and improves system performance.

36
Q

What could be skipped if some operation between file opening and file closing throws an exception?

A

The close operation

37
Q

How do you deal with the close operation being potentially skipped?

A

Put an explicit close method call in a seperate finally block located immediately after the last catch block.

Example:

38
Q

What do all classes that implement closeable also implement?

A

Autocloseable

When an object is autocloseable, instead of closing it with an explicit close statement, we can ask the JVM to close it for us by opening the Autocloseable object with a try-with-resources header