Exceptions Flashcards

1
Q

Happy Path

A

The path through the code where exceptions are not thrown

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

Runtime Exception

A

Tend to be unexpected, but not necessarily fatal.

Also called unchecked exceptions.

Includes RuntimeException and all its subclasses

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

Checked Exception

A

Includes Exception and all subclasses that do not extend RuntimeException

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

Handle or Declare Rule

A

A rule enforced by the compiler that states if a statement throws a checked exception, it must attempt to catch the exception or declare the exception in the method declaration using the throws keyword.

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

throws

A

Indicates that the code might throw the specified exception, but it might not

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

For Runtime Exception…

1) How to recognize?
2) Okay for program to catch?
3) Is program required to handle or declare?

A

1) Subclass of RuntimeException
2) Yes
3) No

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

For Checked Exception…

1) How to recognize?
2) Okay for program to catch?
3) Is program required to handle or declare?

A

1) Subclass of Exception but not subclass of RuntimeException
2) Yes
3) Yes

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

For Error…

1) How to recognize?
2) Okay for program to catch?
3) Is program required to handle or declare?

A

1) Subclass of Error
2) No
3) No

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

try statement syntax

A
try {
   //code
} catch (exception_type e) {
   //code
}

must have the braces!

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

What must go along with a try block?

A

Either a catch block or a finally block.

Note: You don’t need both! You can even just have a try and finally with no catch. You can also have multiple catch blocks

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

If there are multiple catch blocks, you must watch out for….

A

Superclasses being handled before subclasses.

For example, every exception class extends from the class Exception, so if you were to catch an Exception before any others, then the catch statements would be unreachable and the compiler would say no no

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

ArithmeticException

A

Thrown by the JVM when code attempts to divide by 0

RuntimeException

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

ArrayIndexOutOfBoundsException

A

Thrown by the JVM when code uses an illegal index to access an array

RuntimeException

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

ClassCastException

A

Thrown by the JVM when an attempt is made to cast something to a subclass of which it is not an instance

RuntimeException

String type = “moose”;
Integer number = (Integer) type; // DOES NOT COMPILE
More complicated code thwarts Java’s attempts to protect you. When the cast fails at runtime, Java will throw a ClassCastException:

String type = “moose”;
Object obj = type;
Integer number = (Integer) obj;
The compiler sees a cast from Object to Integer. This could be okay. The compiler doesn’t realize there’s a String in that Object.

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

IllegalArgumentException

A

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

Checked Exception

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

NullPointerException

A

Thrown by the JVM when there is a null reference where an object is required

RuntimeException

17
Q

NumberFormatException

A

Thrown by the programmer when an attempt is made to convert a String to a numeric type but the String doesn’t have an appropriate format

18
Q

FileNotFoundException

A

Thrown programmatically when code tries to reference a file that does not exist.

Checked Exception

19
Q

IOException

A

Thrown programmatically when there’s a problem reading or writing a file

Checked Exception

20
Q

ExceptionInInitializerError

A

Thrown by the JVM when a static initializer throws an exception and doesn’t handle it

21
Q

StackOverflowError

A

Thrown by the JVM when a method calls itself too many times

22
Q

NoClassDefFoundError

A

Thrown by the JVM when a class that the code uses is available at compile time but not at runtime

23
Q

3 ways to print an exception called e

A

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