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?

25
Who throws ClassCastException?
ClassCastException
26
Who throws IllegalArgumentException?
programmer
27
Who throws NullPointerException?
JVM
28
Who throws NumberFormatException?
programmer
29
What is ArithmeticException
divide by zero
30
ArrayIndexOutOfBoundsException
int[] countsOfMoose = new int[3]; System.out.println(countsOfMoose[-1]); There is no such thing as negative array index
31
ClassCastException
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
IllegalArgumentException
IllegalArgumentException is a way for your program to protect itself. Like if you dont want negative values
33
NullPointerException
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
NumberFormatException
trying to convert something non-numeric into an int: Integer.parseInt("abc");
35
FileNotFoundException
Checked exception. Programmer a file does not exist
36
IOException
problem reading or writing a file
37
what is FileNotFoundException subclass of?
IOException
38
Errors thrown by what?
JVM
39
ExceptionInInitializerError
Thrown by the JVM when a static initializer throws an exception and doesn’t handle it
40
StackOverflowError
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
NoClassDefFoundError
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
What are checked exception?
Checked exceptions must be handled or declared.
43
Does this compile? ``` class CanNotHopException extends Exception { } class Hopper { public void hop() { } } class Bunny extends Hopper { public void hop() throws CanNotHopException { } } ```
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
System.out.println(e);
the exception type and message
45
System.out.println(e.getMessage());
just the messagve
46
e.printStackTrace();
The rest shows a stack trace
47
When are you required to use a finally block in a regular try statement (not a try-withresources)?
when there is no clause
48
Why are checked exception called checked exception?
Checked during the compilation.
49
Checked Exception is the subclass of? java.lang.Exception or java.lang.RuntimeException?
java.lang.Exception
50
nmame runtime exceptions?
NullPointerException, ArrayIndexOutOfBoundsException, ClassCastExceptions.
51
why called runtime exception?
it isnt feasible to determine whether a method call with throw a runtime exception until it executes.
52
default value of static and instance variable
null