Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 22 March 2017

Exception Handling in Java

 

Java Exception Handling

The exception handling in java is the way to handle run-time errors so that the normal flow of the application can be maintained. Exception handling is the most powerful feature of java.

First we understand what is exception?


What is exception

If we say exception in other word is "abnormal" condition which arise in application. Java Exception is an event that disrupts the normal flow of the program and an exception is an object which is thrown at run-time.


What is exception handling

Exception handling in java is used to control the normal flow of the program by using the exception handling in program. Exception handling is used to handle run-time errors such as IO, SQL, ClassNotFound, etc. 


Why use exception handling in java

  • To maintain the normal flow of the program
  • To convert the system error generated massages into user friendly error massages.

For example:

Suppose there is 5 statements in your program and there occurs an exception at statement 3 and rest of the statements or code will not be executed i.e 4 to 5 statements will not execute. If we use exception handling, rest of the statements will execute perfectly. That is why we use exception handling.

statements 1;
statements 2;
statements 3;//exception occurs
statements 4;
statements 5;

Exception Handling in java
In the above diagram, Object is the top most class in java, all the classes extends Object class by default in java. It is super class in java. We will learn in detail in later about Object class.

Here Throwable is  top most class of exception classes and it is located in lang package.


Types of Exception

There are mainly two types of exceptions,where error is considered as unchecked exception
  1. Checked Exception
  2. Unchecked Exception
  3. Error

Checked Exception

Checked exception are the exception which checked at compile-time e.g SQL Exception, IO Exception, etc.

Unchecked Exception

Unchecked exception are not checked at compile-time, Unchecked exception are checked at run-time e.g ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.


Error

Error is irrecoverable e.g VirtualMachineError, AssertionError, OutOfMemoryError, StackOverflow, etc.


There are many reasons for Exception

  • Open a non existing files
  • Network Connection Problems

Common scenarios where exceptions may occurs

There are given some scenarios where unchecked exception may occurs, these are:

(1) ArithmeticException 

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

For example:

int number = 32/0;//ArithmeticException

(2) NullPointerException

If we have null values in any variables.

For example:

String a = null;
System.out.println(a.length());//NullPointerException

(3) ArrayIndexOutOfBoundsException

If you are inserting any value in the wrong index, there occurs an ArrayIndexOutOfBoundsException.

For example:

int a[] = new int[5];
a[6]=10;//ArrayIndexOutOfBoundsException

(4) NumberFormatException

The wrong formatting of any values, suppose i have a string variable that have characters, converting this variable into digit will occur NumberFormatException.

For example:

String s = "xyz";
int a = Integer.parseInt(s);//NumberFormatException


Difference between Error and Exception 

Error
  • Error cannot be handle
  • For example NoSuchMethodError, OutOfMemoryError, JVM error

Exception
  • Exception can be handle easily
  • For example ArrayIndexOutOfException, ArithmeticException


Java Exception Handling Keyword 

To maintain the normal flow of the program we are going to use some useful keywords so that we can easily handle the exceptions.
  • try
  • catch
  • finally
  • throw
  • throws
learn in next chapter
Share:

4 comments:


  1. I am happy to find much useful information in the post, writing sequence is awesome, I always look for quality content, thanks for sharing.

    Oil tanker ladders manufacturers

    ReplyDelete
  2. Thanks for this valuable information about Exception Handling in Java. Keep Sharing this information.

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate