by Dinesh Thakur Category: Exception Handling

The main advantages of the exception-handling mechanism in object oriented programming over the traditional error-handling mechanisms are the following:

 
by Dinesh Thakur Category: Exception Handling

Redirecting exceptions using throws

Recall that the code capable of throwing an exception is kept in the try block and the exceptions are caught in the catch block. When there is no appropriate catch block to handle the (checked) exception that was thrown by an object, the compiler does not compile the program. To overcome this, Java allows the programmer to redirect exceptions that have been raised up the call stack, by using the keyword throws. Thus, an exception thrown by a method can be handled either in the method itself or passed to a different method in the call stack.

 
by Dinesh Thakur Category: Exception Handling

An exception handler designed to handle a specific type of object may be preempted by another handler whose exception type is a super-class of that exception object. This happens if the exception handler for that exception type appears earlier in the list of exception handlers. That is, while using multiple catch statements, it is important to be aware of the order of exception classes and arrange them correctly.

 
by Dinesh Thakur Category: Exception Handling

Based on the severity of the error, Exception classes are categorized into two groups: unchecked exceptions and checked exceptions. Unchecked exceptions are those that can be either handled or ignored. If the programmer ignores an unchecked exception, the program will terminate when such an error occurs. On the other hand, if a handler is provided for an unchecked exception, the result of the occurrence of such an error will depend on the code written in the exception handler. An example of an unchecked exception is the class RuntimeException (and its sub-classes), which is a sub-class of the class Exception. It is a very important class in Java programming.

 
by Dinesh Thakur Category: Exception Handling

Constructors

There are four constructors in the Throwable class:

• Throwable ()

• Throwable (String message)

 
by Dinesh Thakur Category: Exception Handling

When a condition causes an exception to be thrown, that exception is thrown either by the Java virtual machine or by the Java throw statement. Exceptions are represented by objects instantiated from the class java.lang. Throwable or one of its sub-classes. Java also allows users to define classes that can throw their own (user-defined) exception objects as needed in their design; however, this new class must extend the throwable class or one of its sub-classes.



 
by Dinesh Thakur Category: Exception Handling

Java.lang.Exception Class is the superclass for all exceptions in Java. To creating our own extensions, we simply extend this class.

 
by Dinesh Thakur Category: Exception Handling

ArrayStoreException is thrown when ever we storing something in an array. If we store wrong type object into an array then this will be thrown. Here is the Java example which explains this.

 
by Dinesh Thakur Category: Exception Handling

Sometimes a catch block catches one exception type and then throws a new exception with additional information. This is known as chained exception. This feature allows you to associate another exception with an exception. The second exception describes the cause of the firstexception.



 
by Dinesh Thakur Category: Exception Handling

So far you have been using the pre-defined exception classes provided by the Java API. However, if you encounter a problem that cannot be adequately described by the predefined exception classes, you can create your own exception class.

 
by Dinesh Thakur Category: Exception Handling

So far we have been using just single try statement. However, it is possible to nest a try statement inside another try statement. If one try block does not have a corresponding catch block that handles the exception, Java will search the next outer try block for a catch block that will handle the exception, back through successive nesting. If the Java cannot find the catch block for the exception, it will pass the exception to its default exception handler.

 
by Dinesh Thakur Category: Exception Handling

If an exception occurs inside a try block and there is no matching catch block, the method terminates without further executing any lines of code from that method. For example, suppose you may want to close a file that has been opened even if your program could not read from the file for some reason. In other words, you want to close the file irrespective whether the read is successful or not. If you want that some lines of code must be executed regardless of whether or not there is matching catch block, put those lines inside the finally block.

 
by Dinesh Thakur Category: Exception Handling

The basic principle of Java error handling mechanism is that an exception must either be handled by the method in which it is raised or passed along the call chain for another method to handle it. Suppose a method throws an exception, that is neither a subclass of RuntimeException nor of Error i.e. if it throws a checked exception, then Java requires that the method either handles it or declares it. If the method does not handle the checked exception then the method must declare it using the throws keyword.

 
by Dinesh Thakur Category: Exception Handling

There might be situations in your program where you want to both catch an exception in your code and also want its caller be notified about the exception. This is possible by rethrowing the exception using throw statement.

 
by Dinesh Thakur Category: Exception Handling

You can also throw an exception explicitly. This is accomplished using the throw statement. A throw statement is executed to indicate that an exception has occurred. The exception that you throw using the throw statement can either be the standard system exception or the one that are created by you. The syntax of throw statement is as follows,

 
by Dinesh Thakur Category: Exception Handling

The Throwable class provides the following commonly used methods.

String getMessage (): It returns the description of the exception. It includes fully qualified name of the Exception class and a brief description of the exception.

 
by Dinesh Thakur Category: Exception Handling

When an exception is thrown in your program, you can find the exact statement in your program that caused the exception to occur by examining the lines that are displayed right after the line that indicates which exception was encountered. These lines of information that are displayed when an exception occurs is known as Stack trace. It lists the different methods that the exception passed through before your program was completely aborted. Each line in the stack trace contains not only the method name and the corresponding classname but also the name of the source file that contains the class and the line number where the exception occurred.



 
by Dinesh Thakur Category: Exception Handling

Typically, code in a try-block can throw more than one kind of exception. If this is the case then you can put several catch blocks after the try block to handle them, one for each possible exception. When an exception is generated, the JVM searches the catch blocks in order. The first catch block with a parameter that matches the exception thrown will execute, any remaining catch blocks will be skipped.

 
by Dinesh Thakur Category: Exception Handling

Exception handling is a technique of processing problems that occur during the execution of the program. Using exception handling, we can test the code and avoid it from exiting abruptly.

 
by Dinesh Thakur Category: Exception Handling

A java exception can be thrown only in the following three scenarios:

 

(1) An abnormal execution condition was synchronously detected by the Java virtual machine.



 

Page 1 of 2



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.