Exceptions and Errors in Java


Exception is an exceptional case that can happen in a program

An Exception occurs during the execution of a program and disrupts the normal flow of instructions.

When there is an exception

  • Normally the program crashes and prints a system generated error message
  • Not acceptable in a mission critical application

The programmer can handle the exception, preventing the program from crashing

Exception Handler is a set of instructions that handles an exception

The Java programming language provides a mechanism to help programs report and handle errors

When an error occurs, the program throws an exception

The exception object that is thrown contains information about the exception

The runtime environment attempts to find the Exception Handler

  • The exception handler attempts to recover from the error
  • If the error is unrecoverable, provide a gentle exit from the program after clean up operations like closing open files etc

Helpful in separating the execution code from the error handler

An exception occurs when one part of a system is unable to do what it was asked to do

  • All it can do is to throw an exception indicating that something has gone wrong

Another part of the program can handle the exception

  • It should catch the exception and handle it




Exceptions are situations within the control of an application, that it should try to handle



Errors indicate serious problems and abnormal conditions that most applications should not try to handle


Exception in Java


All exceptions are represented as objects in Java

  • When an exception is to be thrown in a Java program, one of these objects is thrown

Java cannot throw any object, it can throw only Throwable objects

The Java library has a class Throwable which is at the top of the hierarchy of exception classes

Exception thrown by Runtime


Some exceptions are thrown by the runtime

  • When a Java program tries to divide by zero, the runtime will create and throw an object of ArithmeticException
  • When a Java program tries to access an element outside the boundary of an array, the runtime will create and throw an object of ArrayIndexOutOfBoundsException
  • When a Java program tried to use a reference that is not referring to an object, the runtime will create and throw an object of NullPointerException


Checked and Unchecked Exception

Runtime Exceptions are also known as Unchecked Exceptions as the compiler will not check whether the programmer has handled them or not

All exceptions other than Runtime Exceptions are also known as Checked exceptions as the compiler will check whether the programmer has handled them or else generate a compilation error

Few Tips,

Ideally a catch block should solve the problems created by an exception so that the program can continue its execution. But very often a catch block may not be able to completely solve the problems caused by the exception. In such cases, the catch block may do a clean up operation like closing all open files, print an appropriate error message and gently exits from the program. This is far more better than the program printing a system generated error message and crashing without doing any clean up operations.

In Java, to invoke a method, the programmer needs to know the name of the method, list of parameters, the return type and the Checked Exceptions being thrown by the method.

Java syntax mandates the programmer to list all the Checked Exceptions that are being thrown from a method using the throws clause.

Leave a Reply

Your email address will not be published. Required fields are marked *