Chapter 6: exceptions Flashcards

1
Q

A(n) ____ exception happens during runtime.

A

unchecked

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

What’s the difference between an exception and compiler error?

A

All exceptions happen at runtime. Compiler error happens before the program is run.

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

A runtime exception is also known as:

A

unchecked exception

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

A _____ exception inclucdes Exception and all subclassed that do not extend Runtime Exception

A

checked exception

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

Is this a checked or unchecked exception?

void fall() throws Exception{

throw new Exception();

}

A

Checked. Because we tell the program how to handle the exception be declaring.

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

What does the ‘throw’ keyword indicate when used in a program?

A

Just that this method might throw an exception

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

Is the NullPointerException checked or unchecked?

A

unchecked. We didn’t tell the program what to do and it happened at runtime.

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

How do you recognize a Runtime exception? What will it’s super class be?

A

RuntimeException

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

Does Java force you to explicit handle a runtime exception with something like a throws() or try catch?

A

No

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

How do you recognize a checked exception? What will it’s super class be?

A

It will be a subclass of Exception but not of RuntimeException.

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

Does Java force you to handle or declare a checked exception?

A

yes

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

How do you recognize an error exception? What will it’s superclass be?

A

Subclass of Error

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

Will the program catch an Error (are errors fatal?), and are you required to handle or declare an exception to handle an Error.

A

Errors are fatal. Errors are not declared.

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

What does Java use ‘try’ statements for?

A

To separate the logic that might throw an exception from the logic to handle that exception.

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

Are curly braces required in a try catch statement?

A

Yes.

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

What happens to the catch block of code if the code in the try block doesn’t fail?

A

It doesn’t run.

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

Are the curly braces in a try catch required around both the try block and the catch clause?

A

Yes

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

Why doesn’t this compile?

try

fall();

catch(Exceptione e)

System.out.print(“Get up”);

A

No. the try block requires curly braces

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

Does this compile?

try{

fall();

}

A

No. You must have a catch statement in a try/catch block.

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

When does the finally{} clause execute?

A

The finally clause executes no matter what in a try catch statement. If the try is successful, the finally still runs. If the try is unsuccessful and the catch clause runs, the finally block will still run.

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

Does finally have to follow a catch statement in a try catch block?

A

yes. if the catch doesn’t precede the finally the code will not compile.

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

Are you allowed to have multiple catch statements in a try/catch?

A

Yes.

23
Q

Let’s say you have a try catch block with custom exceptions. Or, at least you are using different kinds of exceptions, some of which inherit from another. What happens if you run a subclass exception that runs before an exception using a superclass of that subclass as the exception.

A

This is just fine. As long as the subclass exception runs before the super this will be fine. but if the super runs before the sub, the compiler will indicate that some code is out of reach.

24
Q

Remember, at most one catch block will run and it will be the first catch block that can handle it.

A
25
Q

Which three types of exceptions do you need to be able to recognize for the OCA exam?

A

Runtime (unchecked) exceptions, check exceptions, and errors.

26
Q

Do runtime exceptions have to be handled or declared?

A

No

27
Q

Can runtime exceptions be thrown by both the programmer and the jvm?

A

yes

28
Q

Why would ArithmeticException be thrown?

A

Thrown by the JVM when the code attempts to divide by zero

29
Q

What causes an ArrayIndexOutofBoundsException to be thrown?

A

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

30
Q

Why would a ClassCastException be thrown?

A

This is thrown by the JVM when an attempt is made to cast an object to a subclass of which it is not an instance.

31
Q

Why would an IllegalArgumentException be thrown?

A

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

32
Q

What causes a NullPointerException?

A

This is thrown by the JVM when there is a null reference where an object is required.

33
Q

Why would a NumberFormatException be thrown?

A

This is thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have the appropriate format.

34
Q

Which exception would be thrown?

int answer = 11/0;

A

ArithmeticException

35
Q

Which exception would be thrown by this code?

int[] countsOfMoose = new int[3];

System.out.println(counstOfMoose[-1]);

A

ArrayIndexOutofBoundsException

36
Q

Which exception would be thrown by the code?

String type = “moose”;

Integer number = (Integer) type;

A

ClassCastException

37
Q

Which exception would be thrown by this code?

String name;

public void printLength() {

System.out.println(name.length());

}

A

NullPointerException. name doesn’t have a value (other than null) so it cannot print out a length.

38
Q

Which exception would be thrown by this code?

Integer.parseInt(“abc”);

A

NumberFormatException

39
Q

Checked exceptions have Exception in their hierarchy but not _____

A

RuntimeException

40
Q

Checked exceptions must be ____ or _____.

A

Handled or declared

41
Q

Checked exceptions can be thrown by the _____ or the _____.

A

Checked exceptions can be thrown by the programmer or the jvm.

42
Q

Which checked exception is thrown when code tries to reference a file that does not exist?

A

FileNotFoundException

43
Q

Which exception is thrown programmatically when there’s a problem reading or writing a file?

A

IOException

44
Q

FileNotFoundException is a child of ______

A

IOException

45
Q

Errors extend the ____ class

A

Errors

46
Q

Errors are thrown by the _____ and should not be _____ or ______.

A

Jvm. Handled or declared

47
Q

Which error is thrown by the JVM when a static initializer throws an exception and doesn’t handle it?

A

ExceptionInInitializerError

48
Q

Which error is thrown by infinite recursion?

A

StackOverflowError

49
Q

Which error is thrown by the JVM when a class that the code uses is available at compile time but not at runtime?

A

NoclassDefFoundError

50
Q

Java runs ____ initializers the first time a class is used.

A

Static initializers

51
Q

Which two exceptions will this code throw?

static { int[] countsOfMoose = new int{3};

int num = countsOfMoose[-1];

}

public static void main(String[] args){}

A

ExceptionInInitializerError caused by ArrayIndexOutofBoundsException

52
Q

Which exception will this code throw?

public static void whatever(int num){

whatever(1);

}

A

StackOverFlowError as this is infinite recursion

53
Q

Why doesn’t this compile?

class CanNotHopException extends Exception{}

class Hopper{public void hop(){}}

class Bunny extends Hopper{

public void hop() throws CanNotHopException{}

}

A

Because Hopper doesn’t extend the CanNotHopException class. A class isn’t allowed to throw any checked exceptions unless the superclass declares them

54
Q

Why wont this compile?

public static void main(String… strings) {

int[] numbers = {1,2,34};

int[] moreNumbers = new int[] {123,4,5,6};

int[] arraySize = new int[4];

try {

System.out.println(numbers[-1]);

} catch (Exception e ){

e.printStackTrace();

} catch (ArrayIndexOutOfBoundsException e1) {

e1.printStackTrace();

}

}

A

Exception is the super class of ArrayIndexOutOfBoundsException, so if Exception is run, then the second code becomes unreachable. Java will find the first exception that will work. In this case the super will catch the problem.