Home | » | Java | » | Advance Java | » | Exception Handling |
---|
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
Exceptions can be thrown by the system or explicitly thrown by the program. Exceptions include errors that could be fatal to a program, and may also include other unusual situations. By managing exceptions, one can manage errors and possibly work around them.
Exceptions in Java are actual objects. They are instances of classes that are inherited from the class Throwable. An instance of a Throwable class is created when an exception is thrown.
The below diagram shows a partial class hierarchy for exceptions.
Throwable has two subclass Error and Exception class. Instances of Error class are internal errors in Java runtime environment( the virtual machine). These error are rare and are usually fatal.
Suvclass of Exception fall into two general groups:
- Runtime exceptions (subclasses of the class RuntimeException) such as ArrayIndexOutofBounds, SecurityException, or NullPointerException.
- Other exceptions such as EOFException and NullPointerException.
Catching Java Exceptions
The following two steps need to be performed to trap an exception:
- Protect the code that contains the method that might throw an exception inside a try block.
- Test for and deal with an exception inside a catch block.
What try and catch effectively means is "try" the code that might cause an exception. If it executes, go on with the program. If it does not execute catch the exception and deal with it".
The catch block should immediately follow the try block.
Example
The following algorithm can explain the try and catch method.
The above example explains the algorithm of opening a file. The opening of the file could potentially throw an exception. To handle this exception, the code is enclosed inside a try block.
As the try block is likely to throw any file related error, an associated catch block traps a file related exception.
The following example is an applet called ExecpApp with two text fileds, When a number is entered in each of the text fields and the Enter key is pressed, the program divides the first number by the second and display the result (dropping any remainder). the following example does not contain any exception-handling code.
C:\>jdk1.4\bin>javac ExcepApp.java
C:\>jdk1.4\bin>appletviewer ExcepApp.html
"Output of ExcepApp.class" Incase the user presses the Entery key when either of the text boxes are empty or enteres an invalid numerical value, then Java throws a NumberFormatException. |
Incase of Division by zero, refer the below diagram for th ArithemeticException raised. |
"Division by zero" |
Arithmetic Exception raised incase of division by zero Incase of a valid number being entered, the result after the division of numbers is shown in below diagram. |
Catching Java Exceptions | Catching a RunTime Exception |
Handling Multiple Exceptions | The finally Clause |
Creating User-defined Exceptions |
Hi,Great write-up and very easy to understand,All the topics are covered and thanks for this blog.
ReplyDeleteDot Net Training Institute in Chennai
Software Testing Course in Chennai
Core Java Training in Chennai
PHP Certification in Chennai
Java Exception Handling method have easy understood me.Thanks.
ReplyDeleteJava Training with Placements
ReplyDeleteI really appreciate this stuff.it is very amazing content.thanks for sharing such a useful stuff.it really helped me very much to solve many proble.
Aluminium Telescopic ladders manufacturers
Very informative post about Exception Handling in Java. Keep Sharing.
ReplyDelete