Disruption during the execution of the program is referred as error or exception. In other words, an exception is any event that interrupts the normal flow of execution.

There are two types of errors,

  • Compile time errors
  • Runtime errors

Java Exception Handling

Compile time errors can be again classified again into two types.

Compile time error

Run time error

  • Syntax Errors

    Example: Integer Int a;

    you mistakenly declared it as

    in a;

    for which compiler will throw an error.

  • A Runtime error is called an Exceptions error. It is any event that interrupts the normal flow of program execution.

    Example for exceptions are, arithmetic exception, Nullpointer exception, Divide by zero exception, etc.

    Exceptions in java are something that are out of developers control.

  • Semantic Errors

    Example: You have declared a variable (int a;) and after some lines of code you again declare an integer as (int a;). All these errors are highlighted when you compile teh code.

Suppose you have coded a program to access the server. Things worked fine while you were developing the code.

Please be patient. The Video will load in some time. If you still face issue viewing video click here


Java Exception Handling

During the actual production run, the server is down. When your program tried to access it, an exception is raised.

Java Exception Handling

How to handle exception

So far we have seen what exception is.

But blaming your code failure on environmental issues is not a solution. You need a Robust Programming, which takes care of exceptional situations. Such code is known as Exception Handler.

In our example, good exception handling would be, when the server is down connect to the backup server.

Java Exception Handling

To implement this, enter your code to connect to the server (Using traditional if and else conditions).

You will check if the server is down.

If yes write the code to connect to the backup server.

Such organization of code, using "if" and "else" loop is not effective when your code has multiple java exceptions to handle.

Java Exception Handling

Hence, Java provides an inbuilt exceptional handling.

  1. The normal code goes into TRY block.
  2. The exception handling code goes into the CATCH block

Java Exception Handling

In our example, TRY block will contain the code to connect to the server. CATCH block will contain the code to connect to the backup server.

In case the server is up, the code in the CATCH block will be ignored. In case the server is down, an exception is raised, and the code in catch block will be executed.

Java Exception Handling

So, this is how the exception is handled in Java.

A Summary of the points covered in the above demonstration –

  1. An Exception is a run-time error which interrupts the normal flow of program execution.
  2. A robust program should handle all exceptions and continue with its normal flow of program execution.
  3. Exception Handler is a set of code that handles an exception.

Exceptions can be handled in Java using try & catch.

Sytanx for using try & catch

try{
       statement(s)
      }
      catch (exceptiontype name){
       statement(s)
      }

Step 1) Copy the following code into an editor

class JavaException {
   public static void main(String args[]){
      int d = 0;
      int n =20;
      int fraction = n/d;
     System.out.println("End Of Main");
   }
}

Step 2) Save the file & compile the code. Run the program using command, java JavaException.

Step 3) An Arithmetic Exception - divide by zero is shown as below for line # 5 and line # 6 is never executed.

Step 4) Now lets see examine how try and catch will help us handle this exception. We will put the exception causing line of code into a try block , followed by a catch block. Copy the following code into editor.

class JavaException {
	public static void main(String args[]){
		int d = 0;
		int n =20;
	try{
		int fraction = n/d;
		System.out.println("This line will not be Executed");
		}
	catch(ArithmeticException e){
		System.out.println("In the catch Block due to Exception = "+e);
		}
	System.out.println("End Of Main");
	}
}

Step 5) Save , Compile & Run the code.You will get the following output

Java Exception Handling

As you observe , the exception is handled and the last line of code is also executed. Also note that Line # 7 will not be executed because as soon as an exception is raised flow of control jumps to the catch block .

Note:The AritmeticException Object "e" carries information about the exception that has occurred which can be useful in taking recovery actions.

Java Finally Block

The finally block is executed irrespective of an exception being raised in the try block.It is optional to use with a try block.

 try{
       statement(s)
      }

      catch(ExceptiontType name){

       statement(s)

      }

      finally{

       statement(s)

      }

In case, an exception is raised in the try block, finally block is executed after the catch block is executed.

Assignment

Step 1) Copy the following code into an editor.

class JavaException {
   public static void main(String args[]){
    try{
       int d = 0;
       int n =20;
       int fraction = n/d;
    }
  catch(ArithmeticException e){
    System.out.println("In the catch clock due to Exception = "+e);
  }
  finally{
  System.out.println("Inside the finally block");
  }
}
}

Step 2) Save , Compile & Run the Code.

Step 3) Expected output. Finally block is executed even though an exception is raised.

Step 4) Change the value of variable d = 1 . Save , Compile and Run the code and observe the output.Bottom of Form

Summary:

  • Disruption during the execution of the program is referred as error or exception.
  • Errors are classified into two categories
    • Compile time errors – Syntax errors, Semantic errors
    • Runtime errors- Exception
  • Java provides an inbuilt exceptional handling method
  • Try block: Normal code goes into this block.
  • Catch block: If there is error in normal code, then it will go into this block

 

YOU MIGHT LIKE: