What is an exception?
Event during execution disrupting normal flow; it is thrown.
Who must detect and handle exceptions?
Programmer
What is an exception handler?
Code section that gracefully responds to exceptions.
What will happen when unhandled exceptions is running during runtime?
Crash a Program
What is exception handling?
Process of intercepting and responding to exceptions to prevent crashes.
From which class are all Error and Exception classes derived?
Throwable.
Which two subclasses derive from Throwable?
What are Error-derived classes for?
Critical JVM errors like out-of-memory; applications shouldn’t handle them.
Which exceptions should programmers handle?
Those derived from Exception class (not Error).
How do you handle exceptions in Java?
Use try block with one or more catch clauses.
What is a try block?
Statements that might throw exceptions; application continues if caught.
What is the syntax of a catch clause?
catch(ExceptionType name) { /* handler */
}What type can catch many exception subclasses?
A parameter of type Exception catches its subclasses.
What errors handles error from Integer.parseInt()?
NumberFormatException
NumberFormatException class is derived from Exception class
How must multiple catch clauses be ordered?
From most specific to most general.
JVM will run the first compatible catch clause found
What happens when the JVM finds a compatible catch?
It runs the first compatible catch clause.
Can we write 2 catch statements with the same Exception type?
No, below is wrong
try{
}catch(NumberFormatException e){
}catch(NumberFormatException e){
}What Java SE 7 feature reduces catch duplication?
Single catch statement can handle multiple exception types (multi-catch).
catch(InputMismatchException | NumberFormatException e){
}What is the finally clause?
Optional block executed after try and catch blocks.
When is finally executed?
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
Will finally execute if exception unhandled?
Yes; finally still executes unless JVM exits prematurely.
Will executes even have error
List out 2 category of exceptions
What are checked exceptions?
Non-Error, non-RuntimeException exceptions checked at compile time.
Occurs in Complie Time