Introduction of Exception handling

Exception handling:- During execution of a program some abnormal conditions (exception situation) might be occurred.

If these abnormal condition will not be handle by the program then abnormal conditions becomes error for the programs. Due to these errors program will be terminated abruptly (sudden). If program handles these abnormal conditions becomes exceptions.In this case program completes execution,this process is called exception handling.

Note:- Java is a robust programming language.Robust means hard to break. This is because of following two features that JVM contains:-

1.    Automatic exception handling

2.    Automatic garbage collection

These are two main reason in java of program termination:-

1.    Memory overflow

2.    Not handle abnormal condition

Exception object:- When JVM incounters any abnormal condition,JVM converts it into an exception.

In Java every exception is represented in the form of object.

In this object JVM store information about the exception such as:-

1.    Type

2.    Region

3.    Method name

4.    Source file of the method

5.    Line number

These information is very useful for the programmer as well as user.

After creating exception object JVM throws that object towards the program to catch it,So that information stored in this object can be show to user.

Catching exception:- This is responsibility of the current program to catch the exception object that is thrown by JVM.

After catching exception object program will continue execution.If program does not catch it then exception will be unhandled.

In the above case program will be terminated by JVM.

Exception handler:- It is a code that is responsible to catch the exception object.This code should be written  by programmer.

Following three tools are given by language to write this code:-

1.    Try

2.    Catch

3.    Finally 

1.    try block:-  These statements of the method that might throw exception should be written inside this block.Every try block must have atleast one finally catch block.



2.    catch block:- This block is executed automatically when statement of the ‘try block’ throws an exception.This block is responsible to store reference of exception object into a variable.

class Exp
public static void main(string [] args)
System.out.println(“Execution begins”);
int n1=Integer.parseInt(s[0]);
int n2=Integer.parseInt(s[1]);
int res=n1/n2;
catch(ArithmaticException ex)
System.out.println(“Arithmatic exception cought”);
catch(ArrayIndexOutOfBoundsException ex)
System.out.println(“Array index exception cought”);
catch(NumberFormatException ex)
System.out.println(“Number format exception cought”);
System.out.println(“Release memory”);

3.Finally block:- Code written inside this block is always executed by the JVM,wheather an exception thrown or not,wheather exception are handle.

You can also visit these related articles and blogs

Exception Handling in Java: try-catch statements 

Exception Handling in Java: Asynchronous Exceptions and Important guidelines

What is Exception handling in java

Last updated:11/30/2017 3:15:05 AM
Harsh kumar srivastava

Harsh kumar srivastava

I am not a cool guy


Leave Comment