Exception Handling in Java

In this tutorial, we will learn about Exception Handling in Java.

What is an Exception ?

An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error.

A Java Exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on.

When an exception occurs in a program, the program execution gets terminated. In such cases we get a system generated error message. In order to avoid termination of the program, the exceptions have to be handled.

Why an Exception occurs ?

Exceptions in Java can occur from different kind of situations that can cause a program to throw an exception. For ex. File not found at specified location,  Index outside the bounds of an array, Network Connection failure, Database connection issues etc.

Types of Exceptions:

All exception types are subclasses of the class Throwable. Hence, Throwable is at the top of the exception class hierarchy. Throwable class is extended by two subclasses Exception and Error.

Exception:

Exception class is used for exceptional conditions that user programs should catch. This is also the class that
you will subclass to create your own custom exception types. Exceptions are further classified into Checked Exceptions and Runtime Exceptions. 

Checked Exceptions:

Checked exceptions are those exceptions which are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.

Unchecked Exceptions:

Unchecked exceptions are those exceptions which are checked at run-time and not at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. All Unchecked exceptions are direct sub classes of RuntimeException class.

Error:

Error defines exceptions that are not expected to be caught under normal circumstances by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself. Stack overflow is an example of such an error.

Exception Handling in Java:

In Java, we can handle these exceptions. By handling the exceptions we can provide a meaningful message to the user about the issue rather than a system generated message, which may not be understandable to a user. Exception handling is a framework that is used to handle runtime exceptions only, compile time exceptions are not handled by exception handling in java.

Let’s see what happens if we don’t handle the exceptions.

ExceptionDemo.java:

Let’s run this program and see the output.

Output:

Here we have seen that, the program has thrown divide by zero an Arithmetic Exception and terminated abruptly. The program has not printed the message in main method. This indicates that if we don’t handle exceptions in Java then the program throws an exception and terminates abruptly.

Let’s see how to handle it.

ExceptionDemo1.java:

Let’s check the output.

Output:

If you notice the output, the program has thrown an exception but didn’t terminate abruptly. The program also printed the message in main method. This is because we have handled exceptions in try-catch block.

I hope now you understand what is exception handling, what happens if we don’t handle it and the advantage that we get if we handle the exceptions properly.

Advantages of Exception Handling:

Exception Handling ensures that the program doesn’t terminate abruptly and execute all the statements in the program without breaking the flow of the program.

Difference between Exceptions and Errors:

ExceptionError
You can recover from an Exception by using either try-catch blocks or throwing exception back to the caller.It is not possible to recover from an Error. The only solution to errors is to terminate the execution.
Exceptions in java are of type java.lang.Exception.Errors in java are of type java.lang.Error.
Exceptions include both checked as well as unchecked type.All errors in java are of unchecked type.
Checked exceptions are known to compiler where as unchecked exceptions are not known to compiler because they occur at run time.Errors occur at run time and not known to compiler.
Examples:
Checked Exceptions:
CloneNotSupportedException, IOException
Unchecked Exceptions:
IndexOutOfBoundsException, ArithmeticException, ClassCastException
Examples:
AnnotationFormatError, AssertionError, OutOfMemoryError

Want to know about throw and throws keyword in Java, then read tutorials Throw keyword in Java and Throws keyword in Java.

Related posts

Leave a Comment