Exception handling is a most frequently used mechanism while developing applications in Java. There are 2 kinds of exceptions in an application:
1. at run time due to careless use of variable/return values some exception might come. This is called RuntimeException. Example of these kind of exceptions are: NullPointerException, DevideByZeroException, ArrayIndexOutOfBoundException etc.
2. During compile time, the application might throw some self defined exceptions for which we must to have either a try-catch block or throws in the method declaration. These time of exceptions are called Checked exceptions. Example of these kind of exceptions are: IOException, SQLException etc.
Throwable
// \\
Error Exception
(Ex: OutOfMemory error) // \\
(Subclass: these RuntimeException
are called Checked
Exception)
If you want to write a Exception for your own project extend the class Exception and handle the Exception in a separate Catch Block.
Example:
class BusinessException extends Exception{
...
}
void method1(){
try{
throw new BusinessException();
}catch(BusinessException be){
//enter Business exception handling logic here
}catch(IOException ioe){
//enter your exception handling logic here
}
}
1. at run time due to careless use of variable/return values some exception might come. This is called RuntimeException. Example of these kind of exceptions are: NullPointerException, DevideByZeroException, ArrayIndexOutOfBoundException etc.
2. During compile time, the application might throw some self defined exceptions for which we must to have either a try-catch block or throws in the method declaration. These time of exceptions are called Checked exceptions. Example of these kind of exceptions are: IOException, SQLException etc.
Exception Hierarchy
Here is the hierarchy for the Exception and Error conditions in Java.Throwable
// \\
Error Exception
(Ex: OutOfMemory error) // \\
(Subclass: these RuntimeException
are called Checked
Exception)
If you want to write a Exception for your own project extend the class Exception and handle the Exception in a separate Catch Block.
Example:
class BusinessException extends Exception{
...
}
void method1(){
try{
throw new BusinessException();
}catch(BusinessException be){
//enter Business exception handling logic here
}catch(IOException ioe){
//enter your exception handling logic here
}
}
No comments:
Post a Comment