Exception Handling Flashcards

1
Q

What is an exception?

A

run-time error

an abnormal condition that arises at runtime.

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

How does exception handling work?

Use the words try, catch, throw, finally

A

Use a try block to monitor for exceptions.
If exception occurs, it is thrown.
The code can catch the exception and handle it in some way.
finally specifies code that will run after the try block

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

How do you define the exception to the thrown from a method?

A

throws

static void throwOne() throws IllegalAccessException {
throw new IllegalAccessException(“demo”)

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

What is the superclass of all exceptions?

A

Throwable

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

What are the two subclasses of Throwable?

A

Exception - normal, in user programs, used to create own exceptions

Error - catastrophic, usually having to do with runtime environment

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

T/F: once an exception is thrown, it must be caught.

A

True.

If not specified, default handler will handle it

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

Do you need to include catch in a try statement?

A

no

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

Whats the goal of a catch clause?

A

resolve the error condition then continue on as if the error never occured.

in contrast to without it, the program would stop.

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

Can you have multiple catch clauses?

A

yes. The execute in order, then stop when one is hit.

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

For multiple catch clauses, Why do exception subclasses must come before their superclass?

A

Because it’ll produce an “unreachable code” error in that the exception will never be able to get to the subclass because it’ll always be caught with the superclass before

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

what is “throw” used for?

A

Can determine the exception to throw instead of the default provided by java

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

Why does a method need to define the exceptions it can throw?

A

so that the method caller can handle it.

Only need define the ones outside of type Error or RuntimeException which are included in java

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

Will a finally block execute even if an exception is not found?

A

yes

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

T/F

think of try, throw, and catch as clean ways to handle errors and unusual boundary conditions in your program’s logic

A

ya

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

in Exception class, what method do you call to get the description of the error?

A

except.getMessage()

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

in Exception class, what method do you call to get the flow of where the error came from?

A

except.printStackTrace()

17
Q

What is Java exception hierarchy of classes

A
18
Q
A