How to Handle Exceptions in Java5 min read

In this article we will look at what is an exception?, what is exception handling?, and how java supports exception handling.

In general, the errors in programs can be categorized into three types:

  1. Syntax errors: Also called compile time errors when a program violates the rules of the programming language. Ex: missing a semi colon, typing a spelling mistake in keyword etc.
  2. Run-time errors: Errors which are raised during execution of the program due to violation of semantic rules or other abnormal behaviour. Ex: dividing a number with zero, stack overflow, illegal type conversion, accessing an unavailable array element etc.
  3. Logic errors: Errors which are raised during the execution of the program due to mistakes in the logic applied in the program. These are the most severe kind of errors to detect. Ex: misplacing a minus sign in the place of plus sign, placing a semi colon at the end of loops etc.

Exception: An abnormal condition that occurs at run-time or a run-time error is known as an exception.

Exception Handling: Handling exceptions is known as exception handling. Instead of letting the program crash (terminate abnormally) when an error occurs at run-time, we provide meaningful error messages to the users by handling the exception(s).

Exception Handling Fundamentals

In Java, abnormal conditions that occur in programs are known as exceptions. Java exceptions are objects which are thrown when an exception raises. Exceptions which are raised must be handled (caught) at some point. Exceptions can be created by Java run-time system or they can be created manually.

Java aids exception handling by providing five keywords: try, catch, throw, throws, and finally. Their use is described as follows:

try: Statements that are supposed to raise exception(s) are placed inside a tryblock.

catch: Code that handles exceptions are placed in catch blocks. A try block must be followed by one or more catch blocks or a single finally block.

throw: Most of the exceptions are thrown automatically by the Java run-time system. We can throw exceptions manually using throw keyword.

throws: If a method which raises exceptions doesn’t want to handle them, they can be thrown to parent method or the Java run-time using the throwskeyword.

finally: All the statements that should execute irrespective of whether a exception arises or not is placed in the finally block.

The general form of an exception handling block looks as follows:

Note that every try block must be followed by one or more catch blocks or a single finally block. To aid exception handling, Java provides several pre-defined exception classes. The root class for all exception classes is Throwable. The hierarchy of exception classes is as follows:

Exception Types

There are different categorizations of exceptions based on factors given below:

  • Nature of exceptions
  • Necessity of handling exceptions
  • Creation of exceptions

Nature of Exceptions:

Based on nature of exceptions, exceptions are categorized into Exceptionsand Errors. Two direct sub classes of the Throwable class are Exceptionand Error.

All the sub classes of Exception class are related to exceptions raised in the programmer’s code. One important sub class is RuntimeException which contains several sub classes representing exceptions that are automatically handled by the Java run-time system. Examples of such exceptions are divide by zero, array index out of bounds.

All the sub classes of Error class are related to exceptions related to problems with run-time system. Examples of such errors are stack overflow and out of memory.

Necessity of Handling Exceptions:

Based on whether it is compulsory to provide exception handling code or not, exceptions are categorized into checked exceptions and unchecked exceptions.

Exceptions for which providing exception handling is not necessary are known as unchecked exceptions. All sub classes of RuntimeException class are examples of unchecked exceptions.

Exceptions which must be handled by the code written by programmer are known as checked exceptions. Examples of checked exceptions are IOException, ClassNotFoundException etc.

Creation of Exceptions

Based on who is providing the exception classes, exceptions are categorized into pre-defined exceptions (system exceptions) and user-defined exceptions.

All exception classes available with Java are known as pre-defined exceptions or system exceptions. Exceptions that created by the programmer are known as user-defined exceptions.

Exception Handling Example

Let’s see a sample Java program which handles “divide by zero” exception. First let’s see the Java program without using exception handling constructs. The program is given below:


The exception which was automatically generated by Java run-time system was ArithmeticException which is a sub class of RuntimeException. So, all unchecked exceptions like ArithmeticException are automatically handled by Java run-time system.

The above output says that the exception was raised in main method, exception raised was ArithmeticException and the associated message is / by zero, exception was raised in Divide class, in file and on line number 6.

Now, let’s use exception handling constructs to provide our own exception handling code (message). The program is given below:

Output of above program is:

This is how we can use exception handling constructs to handle exceptions in Java.

Take your time to comment on our article this will help us improve.

Leave a Comment