Chapter 6 Excaptions Flashcards

1
Q

Tell the subclass of the following exceptions ? Also if they are ok for programmer to catch and is the program required to handle or declare:

  1. Runtime exception
  2. Checked exception
  3. Error
A
  1. Runtime exception - Subclass of RuntimeException - it’s ok for programmer to catch - the program is no required to handle or declare it
  2. Checked exception - Subclass of Exception but not subclass of RuntimeException - it’s ok for programmer to catch - the program is required to handle or declare it
  3. Error - Subclass of Error - it’s not ok for programmer to catch - the program is not required to handle or declare it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is ArithmeticException ?

A

It is a Runtime Exceptions.

Trying to divide an int by zero gives an undefi ned result. When this occurs, the JVM will throw an ArithmeticException:

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

What is ArrayIndexOutOfBoundsException ?

A

It is a Runtime Exceptions.

You know by now that array indexes start with 0 and go up to 1 less than the length of the array—which means this code will throw an ArrayIndexOutOfBoundsException:

int[] countsOfMoose = new int[3];
System.out.println(countsOfMoose[-1]);

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

What is ClassCastException ?

A

It is Runtime Exception.

Java tries to protect you from impossible casts. This code doesn’t compile because Integer is not a subclass of String:
String type = "moose";
Integer number = (Integer) type; // DOES NOT COMPILE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is IllegalArgumentException ?

A

It is Runtime Exception.

This exception indicates that a method is called with incorrect input arguments.

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

What is NullPointerException ?

A

It is Runtime Exception.

Instance variables and methods must be called on a non-null reference. If the reference is null, the JVM will throw a NullPointerException.

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

What is NumberFormatException ?

A

It is a Runtime Exception.

Java provides methods to convert strings to numbers. When these are passed an invalid value, they throw a NumberFormatException. The idea is similar to IllegalArgumentException.

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

What is FileNotFoundException ?

A

It is a Checked Exception.

Thrown programmatically when code tries to reference a fi le that does not exist

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

What is IOException ?

A

It is a Checked Exception.

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

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

What is ExceptionInInitializerError ?

A

It is an Error ?

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

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

What is StackOverflowError ?

A

It is an Error .

Thrown by the JVM when a method calls itself too many times (this is called infinite recursion because the method typically calls itself without end).

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

What is NoClassDefFoundError ?

A

It is an Error.

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

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

Explain differences and similarities between:

java. lang.Error
java. lang.Exception
java. lang.RuntimeException

A

Subclasses of java.lang.Error are exceptions that a programmer should not attempt to handle. Subclasses of java.lang.RuntimeException are runtime (unchecked) exceptions. Subclasses of java.lang.Exception, but not java.lang.RuntimeException are checked exceptions. Java requires checked exceptions to be handled or declared.

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

Which exceptions are thrown by the programmer and which are thrown by the JVM:

  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • ClassCastException
  • IllegalArgumentException
  • NullPointerException
  • NumberFormatException
  • IOException
  • FileNotFoundException
  • ExceptionInInitializerError
  • StackOverflowError
  • NoClassDefFoundError
A

IllegalArgumentException and NumberFormatException are typically thrown by the programmer, whereas the others are typically thrown by the JVM.

Also, java.io.IOException is thrown by many methods in the java.io package, but it is always thrown programmatically.

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

What is System.exit ?

A

There is one exception to “the finally block always runs after the catch block” rule: Java defines a method that you call as System.exit(0);. The integer parameter is the error code that gets returned. System.exit tells Java, “Stop. End the program right now. Do not pass go. Do not collect $200.” When System.exit is called in the try or catch block, finally does not run.

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

Can you handle java.lang.Error ?

A

Legally, you can handle java.lang.Error subclasses, but it’s not a good idea.

17
Q

When you are required to use a finally block in a regular try statement.

A

A try statement is required to have a catch clause and/or finally clause. If it goes the catch route, it is allowed to have multiple catch clauses.