Click here to load reader
Upload
roshan-gunathilake
View
303
Download
2
Embed Size (px)
DESCRIPTION
This presentation provide a detailed introduction about Exceptions and Exception Handling in Java.
Citation preview
EXCEPTIONSException is an occurrence that alters the normal program flow.Program terminates when an exception is thrown.To avoid from this, we must handle the exceptions.Try block & Catch clause.
Normal format of exception handling
try{
risky code
}catch (Name of Exception){
What to do if an exception is thrown
}
Finally Block
Definitely ExecutesTry must be followed by,
1. a catch clause
2. many catch clauses
3. a finally block
4. catch clauses and a finally blockNo code between try and catch
Exception Class
Java.lang.ExceptionAll Exceptions are sub classes of
the class ExceptionCreating our own Exception by
extending the class ExceptionThe Keyword throw
Exception Hierarchy
Throwable
ExceptionsErrors
RuntimeException
StackoverflawError
AssertionError
InterruptedException
ArithmeticException
Errors
Errors represent unusual situations that have not caused by the program.
Normally we cannot recover from them.
So we don’t handle errors.
Using multiple catches &Trys
Catches must be in the same order as exception hierarchy (sub class to super class)
Largest catch must be the last.Some exceptions cannot use without a
risk. eg:-IOException,InterruptedException Multiple trys & trys inside try is possible.
Checked & Unchecked Exceptions
Some Exceptions are checked in compile time.
RuntimeExceptions and it’s subclasses are unchecked.
Others are checked in compile time.The keyword throws is used to guard
risky methods. It’s not handling &just passing the bulk.
Re throwing Exceptions
We can re throw exceptions from the catch clause.
Can give a try-catch from within a catch clause.
Cannot use the same Exception name in re throwing.
JVM & Programmatically thrown Exceptions
JVM exceptions – either exclusively or most logically thrown by JVM.
Eg:- nullpointer exceptionProgrammatic – thrown by application
and/or programmer.Eg:- NumberformatException,
IllegalArgumentException