Exception: An exception is abnormal condition that arises in a code sequence at run time.
A java exception is an object that describes an exceptional condition has occurred in piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused error. That method may chose code to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed.
Java exception handling is managed by five keywords:
try, catch, throw, throws, and finally.
If an exception occurred with in the try block, it is thrown. Our code can catch the exception using catch block and handle it in some relational manner.
System generated exceptions are automatically thrown by the Java run time system.
To manually throw an exception, use the keyword throw.
Any exception that is thrown out of method must be specified as such by a throws clause.
Any code must be executed before the method returns is put in a finally block.
General form of exception handling is as follows:
try{
//block of code to monitor for errors.
}
catch(ExceptionType1 exobj){
// exception handler for ExceptionType1
}
catch(ExceptionType2 exobj){
// exception handler for ExceptionType2
}
//...............................
finally{
//block of code to be executed before try block ends
}
Here ExceptionType is the type of exception that has occurred.
ExceptionTypes: All exceptions types are subclasses of built-in class Throwable.
Immediately below Throwable are two subclasses that partition exceptions into two distinct branches.
One branch is headed by Exception and other is headed by Error.
Error, which defines exceptions that are not excepted to be caught under normal circumstances by your program.
Programs can not handle the Exception of type error, because these are typically created in response to catastrophic failures that can not usually handled by our programs.
A java exception is an object that describes an exceptional condition has occurred in piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused error. That method may chose code to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed.
Java exception handling is managed by five keywords:
try, catch, throw, throws, and finally.
Handling exceptions – Three styles: Altogether, there are three styles of handling the exceptions.
- Using try-catch block; the robust way
- Using throws in place of try-catch, not a robust way
- To throw the exception object to the system using throw keyword, not a robust way
If an exception occurred with in the try block, it is thrown. Our code can catch the exception using catch block and handle it in some relational manner.
System generated exceptions are automatically thrown by the Java run time system.
To manually throw an exception, use the keyword throw.
Any exception that is thrown out of method must be specified as such by a throws clause.
Any code must be executed before the method returns is put in a finally block.
General form of exception handling is as follows:
try{
//block of code to monitor for errors.
}
catch(ExceptionType1 exobj){
// exception handler for ExceptionType1
}
catch(ExceptionType2 exobj){
// exception handler for ExceptionType2
}
//...............................
finally{
//block of code to be executed before try block ends
}
Here ExceptionType is the type of exception that has occurred.
ExceptionTypes: All exceptions types are subclasses of built-in class Throwable.
Immediately below Throwable are two subclasses that partition exceptions into two distinct branches.
One branch is headed by Exception and other is headed by Error.
Error, which defines exceptions that are not excepted to be caught under normal circumstances by your program.
Programs can not handle the Exception of type error, because these are typically created in response to catastrophic failures that can not usually handled by our programs.
To understand exception handling in JAVA, we need to know below categories of exceptions:
Checked exceptions: Checked Exception in Java is all those Exception which requires being catches and handled during compile time. If Compiler doesn’t see any exception handling techniques(try or catch block), it throws Compilation error. All the Exception which are direct sub Class of Exception but not inherit RuntimeException are Checked Exceptions.
Example: If a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
UnChecked/Runtime exceptions: Unchecked Exception in Java is those Exceptions whose handling is not verified during Compile time. Unchecked exceptions probably could have been avoided by the programmer. Unchecked Exceptions mostly arise due to programming errors like accessing method of a null object, invoking method with illegal arguments, ...... In Java, Unchecked Exception is direct sub Class of RuntimeException. What is major benefit of Unchecked Exception is that it doesn't reduce code readability and keeps the client code clean.
Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.
Difference between checked and unchecked exceptions:
- Checked Exception is required to be handled by compile time while Unchecked Exception doesn't. It is mandatory to provide try catch or try finally block to handle checked Exception and failure to do so will result in compile time error, while in case of RuntimeException this is not mandatory.
- Checked Exception is direct sub-Class of Exception while Unchecked Exception are of RuntimeException.
- CheckedException represent scenario with higher failure rate while UnCheckedException are mostly programming mistakes.
Understanding Exceptions:
Any Exception which is subclass of RuntimeException are called unchecked and mandatory exception handling is not requirement for them. Some of the most common Exception like NullPointerException, ArrayIndexOutOfBoundException are unchecked and they are descended from java.lang.RuntimeException. Popular example of checked Exceptions are ClassNotFoundException and IOException and that's the reason you need to provide a try catch finally block while performing file operations in Java as many of them throws IOException.
Checked exception handling is enforced by the compiler, but runtime exceptions are not. Therefore, checked exceptions must to be thrown or handled in order for the code to compile, but there is no such requirement regarding runtime exceptions. Consequently, runtime exceptions belong to unchecked exceptions category along with errors.
Disadvantage with checked exceptions is that the programmer has to handle it even if programmer does not know how to. So, if the programmer just throws a new exception without wrapping the original, the stack trace belonging to the original exception will be lost.
This is where runtime exceptions come in handy. Because all runtime exceptions can be handled in a single place, thus programmers can write less amount of code.
On the other hand, since checked exceptions must be caught, there is no surprise for the programmer. programmer will always know which checked exception could be thrown by a certain method. Contrary to this, various runtime exceptions can be thrown without the knowledge of the programmer.
Generally RuntimeExceptions are exceptions that can be prevented programmatically.
Ex: NullPointerException, ArrayIndexOutOfBoundException.
If you check for null before calling any method, NullPointerException would never occur. Similarly ArrayIndexOutOfBoundException would never occur if you check the index first. RuntimeException are not checked by the compiler, so it is clean code.
Will continue exceptions in next post 2.Exceptions in JAVA.
No comments:
Post a Comment