Exceptions Handling in Java

In java programming, an exception is an abnormal condition that arises or occurs in a source code during run time.

In short  Exceptions are run time errors.


In the above program, Test is the name of a class.

When we try to compile this program we will receive an error message because the value of 42/d (42/0) is infinite and it can’t be possible to save and print infinite value.

Handling Java Exception

In java programwhen exception occurs it  can be managed by the java run time system or it can be manually handled by your code.

Java exception can be managed by 5 keyword  try, catch, throw, throws and finally.

  1. try : Any Program statement that we want to monitor for exception are must be placed  within a try block.
  2. catch  :  If the exception occurs within try block it is thrown. Your code can catch this exception (using catch) and handle it.
  3. throw  :  if exception arises then this system generated exception are automatically thrown by Java run time system. To manually throw an exception, throw keyword used.
  4. throws  :  A throws clause list the type of exception that a method might throw.
  5. finally  :  In programming we want any code must be execute after try block completes  is placed within a finally block.


Note: “try “  block can be used either with catch or finally block or with both but try cannot be used alone.

A try can have multiple catch block.

When there is exception in try block an appropriate catch block is executed based on exception.