articles

Home / DeveloperSection / Articles / What is Exception Handling

What is Exception Handling

Anonymous User3700 20-May-2015

This article teaches what exception handling in your C #programs is.

Introduction:

Exception handling in java is one of powerful mechanism to handle run time errors so that normal flow of the application can be maintained.

If you want to know about Exception handling in details then you learn first what is exception so in this post I’ll teach you both what is exception and what exception handling.

What is exception:

Exception is abnormal condition, In java – exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

What is exception Handling:

Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.

Advantage of Exception Handling:

The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:

  statement 1;  

  statement 2;  

  statement 3;  

  statement 4;  

  statement 5;//exception occurs  

  statement 6;  

  statement 7;  

  statement 8;  

  statement 9;  

  statement 10;

 

Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the exception will be executed. That is why we use exception handling in java.

Hierarchy of java Exception classes:

 What is Exception Handling

Types of Exception:

There are mainly two types of exception: checked and unchecked where error is considered as unchecked exception.

Difference between checked and unchecked exceptions:

Checked Exception: The classes that extend Throwable class except RuntimeException and error are known as checked exceptions  IOException, SQLException etc. checked exception are checked at compile time.

Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. unchecked exceptions are not checked at compile-time rather they are   checked at runtime.

Scenario where ArithmeticException occurs:

If we divide any number by zero there occurs an ArithmeticException.

int a=50/0;//ArithmeticException

 

Scenario where NullPointerException occurs:

If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.

 

String s=null;  

System.out.println(s.length());//NullPointerException   

 

Scenario where NumberFormatException occurs:

The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.

 

String s="abc";  


int i=Integer.parseInt(s);//NumberFormatException  

 

Scenario where ArrayIndexOutOfBoundsException occurs:

 

If you are inserting any value in the wrong index, it would result


ArrayIndexOutOfBoundsException as shown below:

 

int a[]=new int[5];  


a[10]=50//ArrayIndexOutOfBoundsException   

 

Java Exception Handling Keywords:

There are 5 keywords used in java exception handling.

   try

  catch

  finally

  throw

  throws


Check more posts on exception handling here


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By