Handling Exceptions Flashcards

(26 cards)

1
Q

Exceptions represent errors that occur while…

A

…code is executing

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

Java signals an error with…

A

…an Exception object

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

The Exception object includes…

A

…a human readable message and a stack track showing the method calls that led to the error.

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

Java signals an error with an Exception. Then…

A

…the Exception is passed up the call stack until it’s handled.

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

What happens if an Exception is not handled?

A

The program will stop running. This is why it is important to handle exceptions when possible.

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

Two types of exceptions:

  • checked
  • unchecked
A
  • a checked exception is verified by the compiler

- an unchecked exception == a run-time exception, is not verified by the compiler

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

Facts about a checked exception:

A
  • a checked exception is verified by the compiler
  • a checked exception is declared in code as part of a method’s signature
  • a checked exception must be handled, either in the method, or by the code calling the method (enforced by the compiler)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does “handling” an exception mean?

A

the exception is caught and resolved or acted upon

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

what are some ways to handle an exception?

A
  • print or log an error message
  • recovering (means catch and continue, e.g can log then continue?)
  • throwing a new type of Exception
  • polite message to the user to try again
  • automatically retry
  • user-friendly error message and fail
  • user friendly error message and continue
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Adding the exception to the method signature is known as propagating the exception

A

Propagating the exception signals the exception should be passed up the chain to the code that called the method

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

public void write(String str) throws IOException

–when we write code that calls this method…

A

… we must either handle the exception (try/catch) or propagate it (put in our signature or catch it and throw new exception)

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

Facts about unchecked exceptions:

A
  • the Java compiler cannot predict an unchecked e
  • typically a bug and is an unrecoverable error
  • subclass of RuntimeException
  • may be thrown without being declared or handled
  • compiler does require these to be handled at compile time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Some examples of unchecked exceptions

A
  • NullPointerException (access an object that hasn’t been created yet)
  • calculation amounts to zero in the denominator (ArithmeticException)
  • Integer.parse(“not a number”) –> NumberFormatException
  • NumberFormatException is a type of IllegalArgumentException (which is a type of Runtime Exception)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Some notes about try/catch blocks

A
  • tells the compiler that there may be exceptions thrown within the code inside the try block
  • we can choose to 1) put most of a method within the block 2) just the line that may throw an exception 3) create multiple catch blocks for that try 4) create multiple try/catch blocks within the method

It all depends on how we want the code to proceed when an exception occurs

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

a catch block…
catch (Exception e) {
..
}

A

…defines the type of exception being caught, a variable name for that exception to use within the catch block, and code within the block to execute when such an exception occurs in the preceding try block.
One or more catch blocks can be defined after each try block.

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

An exception is thrown.

The runtime will look for a catch block…

A

..matching that type of exception being thrown–> this includes any exception type in an exception’s inheritance chain - THE SUBCLASSES of an exception, not any superclass
…the runtime will only execute the first matching catch block by passing in the exception object
…if not matching catchblock is found, the exception is passed up to the caller of the method that is executing (the next method on the call stack)
…Once the catch block executes, the runtime continues after the try and catch blocks (unless the catch block throws a new error?)

17
Q

How to print the exception user readable error message?

catch (Exception e)

A

System.out.println(e.getMessage());

18
Q
in the following code:
try {
    Order ord=orderDataAccess.get(orderId);
     return fulfullmentService.process(ord)
} catch (SQLException e) {
     System.out.println(.....
}
return false;

why are two lines of code in the try block?

A

because if the orderDataAccess returns a SQLException, we don’t want the fulfillment to be processed.
If it was after the try/catch block, Java would continue with this line after the error is caught.
We do not want to fulfill an order if we are unable to retrieve one from the OrderDataAccess.
(two lines are coupled functionally)

19
Q
try {
    Order ord=orderDataAccess.get(orderId);
     return fulfullmentService.process(ord)
} catch (SQLException e) {
     System.out.println(.....
} catch (NoFulfillerFoundException e) {
    ....
}
return false;
A

Here, the exceptions are handled/logged, and the boolean false is sent back to the calling method. The exceptions are not propagated here.

20
Q

is it a good idea to catch the generic Exception at the end of a try/catch block?

A
No, because we might squash the indication of serious erros that should bubble up, or that should stop the program.
It could hide issues like:
- permission problems
- file system access
- memory problems

Write exceptions to handle specific problems, limit catch blocks to exception types we plan to recover from.

21
Q

Syntax differences between propagating checked and unchecked exceptions

A

checked exception

  1. add the checked exception to my own method’s signature
  2. catch the checked exception and throw a different exception - this different exception must be declared in my own method’s signature IF it is a checked exception

uncheckedexception

  1. it is automatically propagated
  2. catch the unchecked exception and throw a different one - this different exception must be declared in my own method’s signature IF it is a checked exception
22
Q

If an exception is propagated …

A

…execution in that method is done.

23
Q
If we create a subclass of a class that has methods declaring exceptions...
(same for using an interface)
A
...we can't make the declarations (of the thrown exception) more generic 
...the subclass cannot throw new checked exceptions
...subclasses may leave off declared exceptions (may avoid situations where checked exceptions occur)
24
Q

If an exception is something we can recover from in my method…

A

my method should handle it. Leave the program in a state that allows execution to continue.

25
By propagating an exception...
...we are deciding the current method is not the appropriate place to handle the exception, and passing the responsibility of handling the exception further up the calling stack. If no method further up the calling stack handles the exception, the program would crash.
26
At the very least...
we should be logging the exceptions we catch. Don't fail silently.