Exceptions Flashcards

1
Q

What exception does zero division lead to?

A

ArithmeticException

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

What exception does an invalid array index lead to?

A

ArrayIndexOutOfBoundsException

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

What exception occurs from using an object reference that hasn’t been assigned?

A

NullPointerException

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

When does NullPointerException occur?

A

When an attempt is made to used an object reference that hasn’t been assigned to an object

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

What exception occurs when a specific file doesn’t exist?

A

FileNotFoundException

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

How do exceptions occur and what do they contain?

A

The method creates a new exception object which is handed off to the runtime system.

This object contains information about the state of the program when it occur and the method-call stack.

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

What is the hierarchy of Java exceptions?

A

All derive from Throwable.

Errors are those that happen in JVM. They should not be caught be applications as they tend to be unrecoverable.

Exceptions represent situations that occur in a Java program.

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

How are exceptions caught?

A

A method can catch them directly using a try … catch … block.

Otherwise they are passed up the call stack to their calling method, eventually reaching the Java default handler.

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

What is the structure of a try-catch block?

A

try { … } catch (Exception e) { };

n.b. can have multiple catch blocks to handle different exceptions

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

What happens if multiple catch clauses match the exception raised?

A

The first one is matched and handled

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

What exception is raised if a number cannot be parsed?

A

NumberFormatException

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

What is important to note when using multiple catch clauses?

A

Specific ones must come before more general ones - otherwise a compile time error is raised

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

What is the effect of nested try..catch blocks?

A

It allows the handling of errors that occur within the catch clauses, such as if they re throw the exception

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

What is the effect of a finally block?

A

it is guaranteed to be executed. This allows the cleanup of actions that occurred in the try block.

Notably it is still called even if the code in the try block includes return, continue or break

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

How are exceptions triggered?

A

Using the “throw” keyword.

Exceptions are objects so it is common to go: throw new Exception(‘…’);

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

How are methods that raise exceptions declared?

A

With “throws” i.e. public static void method() throws Exception { …

17
Q

How are multiple exceptions declared using throws?

A

As a comma separated list

18
Q

How are exceptions classed?

A

Runtime exceptions occur within Java runtime system: arithmetic exceptions, pointer exceptions and indexing exceptions etc.

Non-runtime exceptions occur in code outside of the java runtime system i.e. during IO

19
Q

In what way is exception handling enforced?

A

The compiler enforces catch-or-declare: methods that use throw must either use a catch block or declare that exception using throws