exxxcepppppppption Flashcards

1
Q

What does Exception and Error extend?

A

Class called Throwable

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

What is the sub class of Throwable?

A

Exception and Error

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

What is the sub class of Exception

A

RunTimeException

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

What are runtime exception? They are also known as?

A

unchecked exceptions.
Exceptions tend to be unexpected but not necessarily fatal

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

What is checked exception?

A

Checked exceptions tend to be more anticipated—for example, trying
to read a fi le that doesn’t exist.

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

java.lang.RuntimeException extends what?

A

java.lang.Exception

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

What is throw?

A

tells Java that you want to
throw an Exception

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

What is throws?

A

throws simply declares that the method might throw an Exception. It
also might not

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

What do you pass in a throw?

A

String parameter with message or can pass no parameters
throw new Exception();
throw new Exception(“Thing”);

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

What type of expection is NullPointerException

A

Run time exception

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

What exception does this produce?
~~~
String[] animals = new String[0];
System.out.println(animals[0]);
~~~

A

ArrayIndexOutOfBoundsException

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

What is error’s subclass of?
is the program required to declare or handle?

A

subclass of Error; No

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

What is checked exception sub class of?
is the program required to declare or handle?

A

Subclass of Exception but not subclass of RuntimeException.
Yes

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

What is run time exception sub class of?
is the program required to declare or handle?

A

Subclass of RuntimeException.
They don’t have to be handled or declared.
They can be thrown by the programmer or by the JVM

No.

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

3: void explore() {
4: try {
5: fall();
6: System.out.println(“never get here”);
7: } catch (RuntimeException e) {
8: getUp();
9: }
10: seeAnimals();
11: }
12: void fall() { throw new RuntimeException(); }

A

fall() calls new RuntimeException and goes directly to line 7

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

Does it compile?
try
fall();
catch (Exception e)
System.out.println(“get up”);

A

No because braces are missing.

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

Does it compile?
try {
fall();
}

A

No because catch is missing

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

catch is not
required if finally is present.

A

true

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

When is finally used?

A

finally is typically used to close resources such as fi les or databases

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

When does finally not get executed?

A

System.exit(0);.

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

When catching exceptions if you have superclass and subclass what should you catch first?

A

subclass
then super class next.

22
Q

Why doesnt the following compile?
~~~

public void visitMonkeys() {
try {
seeAnimal();
} catch (ExhibitClosed e) { //superclass
System.out.print(“not today”);
} catch (ExhibitClosedForLunch e) {// DOES NOT COMPILE subclass
System.out.print(“try back later”);
}
}
~~~

A

Cause the super class took care of it.

23
Q

Who throws ArithmeticException?

A

RuntimeException. JVM

24
Q

Who throws ArrayIndexOutOfBoundsException?

A

JVM

25
Q

Who throws ClassCastException?

A

ClassCastException

26
Q

Who throws IllegalArgumentException?

A

programmer

27
Q

Who throws NullPointerException?

A

JVM

28
Q

Who throws NumberFormatException?

A

programmer

29
Q

What is ArithmeticException

A

divide by zero

30
Q

ArrayIndexOutOfBoundsException

A

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

There is no such thing as negative array index

31
Q

ClassCastException

A

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

32
Q

IllegalArgumentException

A

IllegalArgumentException is a way for your program to protect itself.

Like if you dont want negative values

33
Q

NullPointerException

A

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

34
Q

NumberFormatException

A

trying to convert something non-numeric into an int:
Integer.parseInt(“abc”);

35
Q

FileNotFoundException

A

Checked exception. Programmer
a file does not exist

36
Q

IOException

A

problem reading or writing a file

37
Q

what is FileNotFoundException subclass of?

A

IOException

38
Q

Errors thrown by what?

A

JVM

39
Q

ExceptionInInitializerError

A

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

40
Q

StackOverflowError

A

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

41
Q

NoClassDefFoundError

A

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

when Java can’t find the class at runtime

42
Q

What are checked exception?

A

Checked exceptions must be handled or declared.

43
Q

Does this compile?
~~~
class CanNotHopException extends Exception { }
class Hopper {
public void hop() { }
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { }
}
~~~

A

No bc the subclass introduced a new exception
class CanNotHopException extends Exception { }
class Hopper {
public void hop() { }
}
class Bunny extends Hopper {
public void hop() throws CanNotHopException { } // DOES NOT COMPILE
}

44
Q

System.out.println(e);

A

the exception type and message

45
Q

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

A

just the messagve

46
Q

e.printStackTrace();

A

The rest shows a stack trace

47
Q

When are you required to use a finally block in a regular try statement (not a try-withresources)?

A

when there is no clause

48
Q

Why are checked exception called checked exception?

A

Checked during the compilation.

49
Q

Checked Exception is the subclass of?
java.lang.Exception or java.lang.RuntimeException?

A

java.lang.Exception

50
Q

nmame runtime exceptions?

A

NullPointerException, ArrayIndexOutOfBoundsException, ClassCastExceptions.

51
Q

why called runtime exception?

A

it isnt feasible to determine whether a method call with throw a runtime exception until it executes.

52
Q

default value of static and instance variable

A

null