An Error indicates a serious problem that a reasonable application should not try to catch.


The exception indicates conditions that a reasonable application might try to catch. An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

An exception can occur for many different reasons. Following are some scenarios where an exception occurs.

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

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java.

Checked exceptions - A checked exception is an exception that is checked (notified) by the compiler at compilation time, these are also called compile-time exceptions. These exceptions cannot simply be ignored; the programmer should take care of (handle) these exceptions.

Unchecked exceptions - An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.

Errors - These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation. 

Hierarchy of Java Exception classes 

The java.lang.Throwable class is the root class of the Java Exception hierarchy which is inherited by two subclasses: Exception and Error. A hierarchy of Java Exception classes is given below: 


Java Exception Keywords

There are 5 keywords that are used in handling exceptions in Java. 


Java Exception Handling Example

Let's see an example of Java Exception Handling where we using a try-catch statement to handle the exception.

public class JavaExceptionExample{
public static void main(String args[]){
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e)
//rest code of the program
System.out.println("rest of the code...");


Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...