CHp 9 - ExceptionHandling Flashcards

(34 cards)

1
Q

What is an exception?

A

Event during execution disrupting normal flow; it is thrown.

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

Who must detect and handle exceptions?

A

Programmer

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

What is an exception handler?

A

Code section that gracefully responds to exceptions.

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

What will happen when unhandled exceptions is running during runtime?

A

Crash a Program

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

What is exception handling?

A

Process of intercepting and responding to exceptions to prevent crashes.

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

From which class are all Error and Exception classes derived?

A

Throwable.

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

Which two subclasses derive from Throwable?

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

What are Error-derived classes for?

A

Critical JVM errors like out-of-memory; applications shouldn’t handle them.

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

Which exceptions should programmers handle?

A

Those derived from Exception class (not Error).

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

How do you handle exceptions in Java?

A

Use try block with one or more catch clauses.

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

What is a try block?

A

Statements that might throw exceptions; application continues if caught.

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

What is the syntax of a catch clause?

A
catch(ExceptionType name) { /* handler */ 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What type can catch many exception subclasses?

A

A parameter of type Exception catches its subclasses.

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

What errors handles error from Integer.parseInt()?

A

NumberFormatException

NumberFormatException class is derived from Exception class

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

How must multiple catch clauses be ordered?

A

From most specific to most general.

JVM will run the first compatible catch clause found

17
Q

What happens when the JVM finds a compatible catch?

A

It runs the first compatible catch clause.

18
Q

Can we write 2 catch statements with the same Exception type?

A

No, below is wrong

try{
}catch(NumberFormatException e){
}catch(NumberFormatException e){
}
19
Q

What Java SE 7 feature reduces catch duplication?

A

Single catch statement can handle multiple exception types (multi-catch).

catch(InputMismatchException | NumberFormatException e){
}
20
Q

What is the finally clause?

A

Optional block executed after try and catch blocks.

21
Q

When is finally executed?

A

Always after try and catch, whether exception occurred or not.

try{
}catch(){
}finally{
}

System.out.println("Hello");

finally block always be executed
block outside will only be executed if it does not happen any exception

22
Q

Will finally execute if exception unhandled?

A

Yes; finally still executes unless JVM exits prematurely.

Will executes even have error

23
Q

List out 2 category of exceptions

A
  1. Unchecked
  2. Checked
24
Q

What are checked exceptions?

A

Non-Error, non-RuntimeException exceptions checked at compile time.

Occurs in Complie Time

25
Give examples of checked exceptions.
1. IOException 2. SQLException, 3. ClassNotFoundException.
26
What are unchecked exceptions?
Runtime exceptions from programming bugs; occur at runtime. ## Footnote Occurs in **Run Time**
27
What's the difference: throws vs throw?
1. throws declares method may throw 2. throw actively throws an exception.
28
Give examples of unchecked exceptions. ( 4 )
1. IllegalArgumentException 2. NullPointerException 3. ArithmeticException 4. ArrayIndexOutOfBoundsException
29
What must a method do if it can throw checked exceptions?
Handle them or declare with throws in method header.
30
How to manually throw an exception?
throw new ExceptionType("message");
31
How to create a custom checked exception?
Extend Exception class.
32
How to create a custom unchecked exception?
Extend RuntimeException class. ## Footnote ``` public class NegativeValueExceptions extends RuntimeException{ public NegativeValueExceptions(){ super("Negative Value"); } } ```
33
Why create custom exceptions?
Represent domain-specific error conditions clearly.
34
Give bank account custom exception scenarios.
1. Negative balance 2. Negative interest 3. Negative deposit 4. Withdraw exceeds Account's Balance.