Exception Handling in Java

What are Exceptions in Java?

Exceptions in Java are events that occur during the execution of a program and disrupt the normal flow of instructions. When an exception occurs, program execution gets terminated.

Many kinds of errors can cause exceptions:

  • User input errors – This includes any errors caused by the developers.
    Such as typos, or wrong inserted HTTP addresses to which the code should connect.
  • Device errors – Hardware does not always do what you want it to. The printer may be turned off. A web page may be temporarily unavailable. Devices will often fail in the middle of a task. For example, a printer may run out of paper.
  • Physical limitations  – Disks can fill up; you can run out of available memory.
  • Code errors – A method may not perform correctly. For example, it could deliver wrong answers or use other methods incorrectly. Computing an invalid array index, trying to find a nonexistent entry in a hash table, and trying to pop an empty stack are all examples of a code error.

All these exceptions can be handled in Java.

By exception handling, we can ensure that we do not get the generic error message that is usually received when an exception occurs. We can put some meaningful message that clearly tells us where the error happened. Or we can even write code that will prevent the program from stopping in the event of an error by making another code to continue with execution.

In Java, we have errors and exceptions.

  • Error – Indicates that something severe enough has gone wrong. The application should crash rather than try to handle the error.
  • Exception – These are situations where the program should be written so that it can be recovered.
    It is up to the developers to protect the code as much as possible so that in the event of an exception, either stop the program and write a meaningful message, which can help find the exact cause of the error, or write code that will prevent the program from stopping.

Exception Hierarchy

All exception and error types are subclasses of class Throwable.

We have two branches. One is headed by Exception. This class is used for exceptional conditions that user programs should catch. And another one is headed by ErrorsError is just a problem that arises beyond the control of the user or the developer.


exceptions in java

Types of exceptions in Java

There are two types of exceptions in Java:

  1. Checked Exceptions
  2. Unchecked Exceptions

Checked Exceptions

All exceptions other than Runtime Exceptions are known as Checked exceptions. These kinds of exceptions are checked in compile-time and we are forced to handle those errors.

Some checked exceptions are:

  • IOException – Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. The most common example is reading a file.
  • SQLException – An exception that provides information on a database access error or other errors.
  • ClassNotFoundException – Thrown when an application tries to load in a class through its string name using:
    • The forName method in class Class.
    • The findSystemClass method in class ClassLoader.
    • The loadClass method in class ClassLoader.


Unchecked Exceptions

Exceptions that are derived from the RuntimeException class are known as Runtime or Unchecked exceptions.
These kinds of exceptions are checked in runtime, and we are not forced to handle them because the program is not aware that the error will occur until the moment of execution.

Some unchecked exceptions are:

  • ArithmeticException – Thrown when an exceptional arithmetic condition has occurred. For example, if we try some number to divide by zero.
  • NullPointerException – These exceptions occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NullPointerException.
  • IndexOutOfBoundsException – It is thrown if a program tries to access an array index that is negative, greater than, or equal to the length of the array. 

We can handle exceptions using the try-catch block.  See the next tutorial to learn how to handle exceptions using the try-catch block in Java.

That’s it!


Leave a Reply

Your email address will not be published.