Exception Handling in JAVA
Submitted by moazkhan on Wednesday, February 18, 2015 - 23:27.
Exception Handling in JAVA
In this tutorial you will learn: 1) What is exception in JAVA? 2) What is throw block? 3) What is try and catch block? What is exception in JAVA? Exception is some unusual event that may happen in the program which also requires special attention of the programmer. Dealing with the exceptions require a lot of overhead processing. Not every error has to be indicated by an exception. Exception separates the code that deal with the exception from the code which is executed during normal running of the program. Exception lets us handle things more gracefully. An exception in Java is an object that is created when an abnormal situation arises in your program. This object has members that stores information about the nature of the problem. An exception is always an object of some subclass of the standard class Throwable. Java provides a very well defined hierarchy of exceptions to deal with situations which are unusual. All standard exceptions are covered by two direct subclasses of the class Throwable. Class Error and its subclasses represent conditions that you are not expected to do anything about. ThreadDeath, LinkageError and VirtualMachineError are the direct subclass of the class error. ThreadDeath error is thrown when an executing thread is deliberately stopped and when it is not caught the thread ends. Linkage error arises when we try to create an object of a class that does not exist. VirtualMachineError is thrown when failure of JVM occurs. For all subclasses of Exception Class (except RuntimeException) we have to include code to deal with them. If we believe that our code may generate an error, we must handle it in our method or we may register then our code may throw an exception otherwise our code may not compile. What is throw block? If we have a method which can throw an exception that is neither a subclass of Error nor Runtime Exception, and we do not want to deal with it then you have to at least declare that this exception can be thrown. We can ignore the exception by enabling the method containing the exception throwing code to pass it on.- //Add a throws keyword after the parameter list of the method and list the classes for the //exceptions that might be thrown separated by comma
- { }
Throw , Try and Finally Block
If we need to handle the exception where they occur we use the try, catch and finally blocks. Try block encloses the code that may give rise to one or more exceptions. Code that can throw an exception that you want to catch must be in a try block, Catch block contains the code that is handling the exception of a particular type that may be thrown in a try block. Finally block contains code that is always executed before the method ends regardless of whether any exceptions are thrown in the try block.
- try {
- //code that can throw exceptions
- }
- {
- //code to handle the exception
- }
- finally
- {
- // code to be executed last
- }
- try {
- // Code that can throw exceptions of type ArithmeticException and IndexOutOfBoundException
- }
- // Code to handle exception of either type...
- }
- {
- throw e;
- }
- //The keyword throw is followed by the exception object to be thrown
Add new comment
- 20 views