The main issue every programmer find is the exception handling. Let us together by the means of this article try to see what the best ways available for exception handling are. The idea of the all the programmers like us is to come up with a quality code that instead of making the problems worst, resolves the same at the earliest.
What is an Exception?
Exception is an error which occurs during the execution of a program.
There are mainly two types of exceptions:
1-Checked exception
2-Unchecked exception
Checked exception is considered at compile time so it greatly reduces the occurrence of unhandled exceptions during runtime. They extend Exception and are intended to be handled at compile time. A checked exception indicates an expected problem which might occur during normal application execution.
Following is the list of checked exceptions:
- ClassNotFoundException
- NoSuchMethodException
- NoSuchFieldException
- InstantiationException
- CloneNotSupportedException
- InterruptedException
- IllegalAccessException
Unchecked exceptions extend RuntimeException. An unchecked exception indicates an unexpected problem which occurs due to coding bug. These are exceptions introduced by the application developer during coding. These exceptions are not checked during compile time but they are checked during runtime.
Following is the list of unchecked exceptions
- NullPointerException
- IndexOutOfBoundsException
- ArrayIndexOutOfBoundsException
- ClassCastException
- IllegalStateException
- ArithmeticException
- SecurityException
The Hierarchy of Exception
Handling Exceptions
1-Using try and catch
Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. For example, if you call a method that opens a file but the file cannot be opened, execution of that method will stop, and code that you wrote to deal with this situation will be run. Therefore, we need a way to tell the JVM what code to execute when a certain exception happens. To do this, we use the try and catch keywords. The try is used to define a block of code in which exceptions may occur. This block of code is called a guarded region (which really means "risky code goes here").
Example:
try{
The risky code goes here which is may causes some kind of exceptions. }catch(HandeledException){
Handling of exception goes here
}
2-Using finally
A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. Even if there is a return statement in the try block. The finally block executes right after the return statement, and before the return executes! This is the right place to close your files, release your network sockets, and perform any other cleanup your code requires. If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. If there was an exception thrown, the finally block executes immediately after the proper catch block completes.
Example:
try{
The risky code goes here which is may causes some kind of exceptions.
}catch(Handeled Exception){
Handling of exception goes here
}
finally{
The code goes Here is always excute after the try or the catch blocks.
// Put code here to release any resource we
// allocated in the try clause.
}
Custom Exception Handling
Custom exception handler is designed to meet the application requirement. The normal exception APIs provided by the java are basic building blocks. Now the developer is expected to use these building blocks to make their own custom exception handler.
To define their own custom exception class, developer needs to extend the Exception class. All custom exceptions are considered to be checked exceptions. The custom exception class is like any other normal class but they provide useful methods and directs customized execution path
Following rules needs to be followed to define custom exception class:
- All exceptions must be a child of Throwable.
- Must extend the Exception class for checked exception.
- For runtime exception, extend the RuntimeException class.
Example:
class MyCustomException extends Exception{
// Write useful methods and execution path
}
|
Conclusion
A program can catch exceptions by using a combination of the try, catch, and finally blocks.
- The try block identifies a block of code in which an exception can occur.
- The catch block identifies a block of code, known as an exception handler that can handle a particular type of exception.
- The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block.
The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.
The class of the exception object indicates the type of exception thrown. The exception object can contain further information about the error, including an error message. With exception chaining, an exception can point to the exception that caused it, which can in turn point to the exception that caused it, and so on.
No comments:
Post a Comment