Java – Exception Handling Introduction

Java – Exception Handling Introduction 2017-08-09T13:05:57+00:00

Java Exception Handling:

Exception is a run-time error which arises during the execution of java program. The term exception in java stands for an “exceptional event”.

So Exceptions are nothing but some abnormal and typically an event or conditions that arise during the execution which may interrupt the normal flow of program.

An exception can occur for many different reasons, including the following:

·         A user has entered invalid data.

·         A file that needs to be opened cannot be found.

·         A network connection has been lost in the middle of communications, or the JVM has run out of memory.

“If the exception object is not handled properly, the interpreter will display the error and will terminate the program.

Now if we want to continue the program with the remaining code, then we should write the part of the program which generate the error in the try{} block and catch the errors using catch() block..

Exception turns the direction of normal flow of the program control and send to the related catch() block and should display error message for taking proper action. This process is known as Exception handling.”

The purpose of exception handling is to detect and report an exception so that proper action can be taken and prevent the program which is automatically terminate or stop the execution because of that exception.

Java exception handling is managed by using five keywords: try, catch, throw, throws and finally.

Try:

Piece of code of your program that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown.

Catch:

Catch block can catch this exception and handle it in some logical manner.

Throw:

System-generated exceptions are automatically thrown by the Java run-time system. Now if we want to manually throw an exception, we have to use the throw keyword.

Throws:

If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception.

You do this by including a throws clause in the method’s declaration. Basically it is used for IOException. A throws clause lists the types of exceptions that a method might throw.

This is necessary for all exceptions, except those of type Error or RuntimeException, or any of their subclasses.

All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result.

Finally:

Any code that absolutely must be executed before a method returns, is put in a finally block.

General form:

try {

// block of code to monitor for errors

}

catch (ExceptionType1  e1) {

// exception handler for ExceptionType1

}

catch (ExceptionType2 e2) {

// exception handler for ExceptionType2

}

// …

finally {

// block of code to be executed before try block ends

}

 

Exception Hierarchy:

All exception classes are subtypes of the java.lang.Exception class.

 

The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class.

Errors :

These are not normally trapped form the Java programs.

Errors are typically ignored in your code because you can rarely do anything about an error.

These conditions normally happen in case of severe failures, which are not handled by the java programs. Errors are generated to indicate errors generated by the runtime environment.

For Example :

(1) JVM is out of Memory. Normally programs cannot recover from errors.

(2) If a stack overflow occurs then an error will arise. They are also ignored at the time of compilation.

The Exception class has two main subclasses:

(1)   IOException or Checked Exceptions class and

(2)   RuntimeException or Unchecked Exception class

(1)   IOException or Checked Exceptions :

Exceptions that must be included in a method’s throws list if that method can generate one of these exceptions and does not handle it itself. These are called checked exceptions.

For 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.

Java’s Checked Exceptions Defined in java.lang

 

(2)    RuntimeException or Unchecked Exception :

Exceptions need not be included in any method’s throws list. These are called unchecked exceptions because the compiler does not check to see if a method handles or throws these exceptions.

As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.

Java’s Unchecked RuntimeException Subclasses

Prev Next