Ch11 Exceptions Flashcards

1
Q

What is an exception?

A

An exception is an event that alters program flow.

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

What superclass (aside from Object) do all exceptions inherit from in Java?

A

java.lang.Throwable

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

What is an Error and what should it be used for?

A

Error means something went so horribly wrong that your program should not attempt to recover from it. For example, the disk drive “dissapeared” or the program ran out of memory.

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

What is a checked exception?

A

A checked exception is an exception that must be declared or handled by the application code where it is thrown.

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

What do all checked exceptions inherit from?

A

Checked exceptions all inherit Exception (and thus also Throwable & Object)

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

What is an unchecked exception?

A

An unchecked exception is any exception that does not need to be declared or handled by the application code where it is thrown.

They are also known as runtime exceptions.

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

What do all unchecked exceptions inherit from?

A

Unchecked exceptions all inherit from either RuntimeException or Error.

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

What is the difference between throw and throws?

A

Throw is used as a statement inside a code block to throw a new exception.

Throws is used at the end of a method declaration to indicate what exception it might throw.

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

When is the IOException thrown?

Is it checked or unchecked (runtime)?

A

Thrown programmatically when there is a problem reading or writing a file.

Checked.

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

When is the FileNotFoundException thrown?

Is it checked or unchecked (runtime)?

A

Subclass of IOException thrown programatically when code tries to reference a file that doesn’t exist.

Checked.

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

When is the StackOverflowError thrown?

Is it checked or unchecked (runtime)?

A

Thrown when a method calls itself too many times.

Unchecked.

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

When is the NoClassDefFoundError thrown?

Is it checked or unchecked (runtime)?

A

Thrown when the class that the code uses is available at compile time but not runtime.

Unchecked.

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

Does the following code compile?

try {
} catch (IllegalArgumentException e) {
} catch (NumberFormatException e) {
}

A

No because NumberFormatException is a subclass of IllegalArgumentException meaning it will never reach that catch clause even if NumberFormatException was thrown.

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

Does the following code compile?

try {
} catch (IllegalArgumentException e1) {
System.out.println(e1);
} catch (NumberFormatException e2) {
System.out.println(e1);
}

A

No because e1 is out of scope in the second catch block.

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

Does the following code compile?

try {
} catch (ArrayIndexOutOfBoundsException e | NumberFormatException e) {
}

A

No because only a single identifier is allowed for all exception types.

correct:

try {
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
}

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

Does the following code compile?

try {
} catch (IllegalArgumentException | NumberFormatException e) {
}

A

No because Java intends multi-catch to be used for exceptions that are not related. If it is, it will not compile.

Since NumberFormatException is a subclass of IllegalArgumentException, it won’t compile. The corrent way is just to remove IllegalArgumentException from the clause.

17
Q

What is returned?

try {
return -1;
} catch (Exception e) {
return -2;
} finally {
return -3
}

A

-3

When the return statement is reached in the try block, it executes the finally block first and returns -3 instead.

18
Q

True or false

The finally block is ALWAYS executed.

A

True.

It is always executed, however it may not always finish.

There is one exception: if System.exit() is called in the try block, finally will not be executed.

19
Q

is the following code valid?

try (FileInputStream in = new FileInputStream(“file.txt”)) {
// Read file data
}

A

Yes.

With try-with-resource statements, the catch/finally block is optional.

20
Q

is the following code valid?

try {
// Code
}

A

No.

With try-statements, it is required to either use the catch block or the finally block (optionally you can use both).

The catch/finally block are optional for try-with-resources

21
Q

When using try-with-resources, Java automatically inserts a finally block to close the resource. If another finally block is explicitely written by the programmer, which finally block runs first?

A

The implicit finally block by Java will always run first.

22
Q

What classes are supportable with try-with-resource statements, such that Java automatically closes the resource?

A

Classes that implement the AutoCloseable interface.

This interface contains a void close() method.

23
Q

is the following code valid?

try (FileInputStream in = new FileInputStream(“file.txt”), FileInputStream in2 = new FileInputStream(“file.txt”)) {
// Read file data
}

A

No. Multiple resources must be seperated with a semicolon (;).

24
Q

Is the following code valid?

class CanNotHopException extends Exception { }

class Hopper {
public void hop() {}
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { }
}

A

This code is not valid.

When a class overrides a method from a super-class or implements a method from an interface, it’s not allowed to add new checked exceptions to the method signature.

25
Q

Is the following code valid?

class CanNotHopException extends Exception { }

class Hopper {
public void hop() throws CanNotHopException {}
}
class Bunny extends Hopper {
public void hop() { }
}

A

This is valid.

Subclasses are allowed to declare fewer exceptions than the superclass or interface, because callers are already handling them.

26
Q

Is the following code valid?

class CanNotHopException extends Exception { }

class Hopper {
public void hop() throws Exception {}
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { }
}

A

This is valid.

Subclasses are allowed to declare fewer (in this case its a subclass) exceptions than the superclass or interface, because callers are already handling them.

27
Q

Is the following code valid?

class Hopper {
public void hop() {}
}
class Bunny extends Hopper {
public void hop() throws IllegalStateException { }
}

A

This is valid because IllegalStateException is an unchecked exception.