EXCEPTION HANDLING
Exception handling is an important concept in java to help developers detecting error conditions in code and how to recover from it .There are three kinds of exceptions in java
Checked Exceptions : If you think the end user of the api can recover from an error condition like invalid input , checked exceptions are used . End user of your api will have to handle the checked exceptions by throwing it again or put it in try catch block .
UnChecked Exceptions : Unchecked Exceptions are fault of developers , or java programming mistakes .It has nothing to do with the end user of api . So in unchecked exceptions , neither we need to surround it with try catch nor we need to declare throws clause . All SubClasses of java.lang.RunTimeException
are unchecked exception . for example NullPointerException is a RunTimeException because if the developer checked for null conditions , this exception would not have thrown . So a simple rule is RuntimeExceptions are developer's fault .
Error : Error indicates resource exhaustion and internal errors of jvm , where the program can not procees simply . like the memory is filled up and there is not enough memory available to execute the program then java.lang.outofmemoryerror is thrown and execution is halted abruptly .
java.lang.Throwable is the super class of all the errors and exceptions
Catching The Exception Using Try And Catch
a try catch block is used to handle a exception which might happen during the flow of program . when a exception is thrown the control goes to catch block and then you can take appropriate action.
try {
// code which could throw exception
}
catch(Exception e1) {
//Catch block
//Handle exception like notify user or halt program
}
Multiple catch Blocks:
A try block can have multiple catch block in order to catch different kind of exceptions , but you can not declare a broader exception first then a specific exception .
try {
// code
}
catch(Exception e1) {
//do something
}
catch(ExceptionType2 e2)
{
//do something else
}
catch(ExceptionType3 e3)
{
//do something
}
Feel free to post comments if you have any questions
Exception handling is an important concept in java to help developers detecting error conditions in code and how to recover from it .There are three kinds of exceptions in java
Checked Exceptions : If you think the end user of the api can recover from an error condition like invalid input , checked exceptions are used . End user of your api will have to handle the checked exceptions by throwing it again or put it in try catch block .
UnChecked Exceptions : Unchecked Exceptions are fault of developers , or java programming mistakes .It has nothing to do with the end user of api . So in unchecked exceptions , neither we need to surround it with try catch nor we need to declare throws clause . All SubClasses of java.lang.RunTimeException
are unchecked exception . for example NullPointerException is a RunTimeException because if the developer checked for null conditions , this exception would not have thrown . So a simple rule is RuntimeExceptions are developer's fault .
Error : Error indicates resource exhaustion and internal errors of jvm , where the program can not procees simply . like the memory is filled up and there is not enough memory available to execute the program then java.lang.outofmemoryerror is thrown and execution is halted abruptly .
java.lang.Throwable is the super class of all the errors and exceptions
Catching The Exception Using Try And Catch
a try catch block is used to handle a exception which might happen during the flow of program . when a exception is thrown the control goes to catch block and then you can take appropriate action.
try {
// code which could throw exception
}
catch(Exception e1) {
//Catch block
//Handle exception like notify user or halt program
}
Multiple catch Blocks:
A try block can have multiple catch block in order to catch different kind of exceptions , but you can not declare a broader exception first then a specific exception .
try {
// code
}
catch(Exception e1) {
//do something
}
catch(ExceptionType2 e2)
{
//do something else
}
catch(ExceptionType3 e3)
{
//do something
}
Feel free to post comments if you have any questions