Chapter 11: 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

Is the following code valid?

Exception e = new RuntimeException();
throw e;
A

Yes.

An Exception inherits from the Object class and can therefore be stored in a variable.

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

Is the following code valid?

throw RuntimeException();

A

No. It is missing the new keyword and is thus never instantiated.

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

Is it okay for a program to catch an Error?

A

No. An error is not supposed to be recovered from.

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

When is the ArithmeticException thrown?

Is it checked or unchecked (runtime)?

A

Thrown when code attempts to divide by zero.

Unchecked.

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

When is the ArrayIndexOutOfBoundsException thrown?

Is it checked or unchecked (runtime)?

A

Thrown when code uses an illegal index to access an array.

Unchecked.

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

When is the ClassCastException thrown?

Is it checked or unchecked (runtime)?

A

Thrown when an attempt is made to cast an object to a class of which it is not an instance.

Unchecked.

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

When is the NullPointerException thrown?

Is it checked or unchecked (runtime)?

A

Thrown when there is a null reference where an object is required.

Unchecked.

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

When is the IllegalArgumentException thrown?

Is it checked or unchecked (runtime)?

A

Thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument.

Unchecked.

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

When is the NumberFormatException thrown?

Is it checked or unchecked (runtime)?

A

Subclass of IllegalArgumentException thrown when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format.

Unchecked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
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
19
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
20
Q

When is the ExceptionInInitializerError thrown?

Is it checked or unchecked (runtime)?

A

Thrown when a static initializer throws an exception and doesn’t handle it.

Unchecked.

21
Q

When is the StackOverflowError thrown?

Is it checked or unchecked (runtime)?

A

Thrown when a method calls itself too many times.

Unchecked.

22
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.

23
Q

Does the following code compile? (assuming the methods exist)

try
fall();
catch (Exception e)
getUp();

A

No. Curly braces cannot be ommited with try statements.

Correct would be:

try {
    fall();
} catch (Exception e) {
    getUp();
}
24
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.

25
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.

26
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) {
}

27
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.

28
Q

Does the following code compile?

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

A

Yes it does :)

29
Q

What is the finally block?

A

The finally block is an optional block that always executes at the end, whether or not an exception was thrown.

30
Q

What is the finally block typically used for?

A

Closing resources such as files or databases.

31
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.

32
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.

33
Q

What is the following try-statement called?

try (someResourceHere) {
} catch (IOException e) {
}

A

try-with-resource OR automatic resource management

34
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 block is optional.

35
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).

36
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.

37
Q

Using a try statement, can I add a finally block when no catch block was added?

A

Yes, either catch or finally is required

38
Q

Using a try statement, can I add a second finally block?

A

No, only one finally block is legal.

39
Q

Using a try-with-resource statement, can I add a second finally block?

A

No, only one finally block is legal.

40
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.

41
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 (;).

42
Q

is the following code valid?

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

No, each variable must be declared in a seperate statement.

43
Q

With try-with-resource, when are the resources closed?

A

After the try clause ends and before any catch/finally clauses.

44
Q

Given the following code

try (FileInputStream in = new FileInputStream("file.txt"); FileInputStream in2 = new FileInputStream("file.txt")) {
    // Read file data
}

In what order are the resources closed?

A

in2 first,
in second.

Resources are closed in reverse order.

45
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.

46
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.

47
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.

48
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.

(page 437)