Exception Handling,A powerful feature in java
Exception handling in java:
In this tutorial we will learn about exception and how it can be handled in java(i.e exception handling).
What is an Exception?
Exception is an abnormal condition.
In java, exception is an event that disturbs the normal flow of the program. Once exception is raised the following lines of code after the exception are not executed.One good thing of exceptions is that we can handle the exceptions.First we will see about exceptions and in later part of tutorial we will learn about exception handling.
Example:
Suppose we have a java program containing 10 lines of code.
Exception is an abnormal condition.
In java, exception is an event that disturbs the normal flow of the program. Once exception is raised the following lines of code after the exception are not executed.One good thing of exceptions is that we can handle the exceptions.First we will see about exceptions and in later part of tutorial we will learn about exception handling.
Example:
Suppose we have a java program containing 10 lines of code.
- statement 1;
- statement 2;
- statement 3;
- statement 4;
- statement 5;
- statement 6; //exception occurs
- statement 7;
- statement 8;
- statement 9;
- statement 10;
Assuming that an exception is raised at 6th statement then the following lines after the exception are not executed (from statement 7 to statement 10) and the program execution is terminated.If we apply the concept of exception handling then the lines of code after the exception are executed.
Exception handling is a mechanism which makes the java program to execute smoothly even after exception is raised.
Exception handling is a mechanism which makes the java program to execute smoothly even after exception is raised.
Reasons for exceptions:
There are many reasons for exceptions in a java program.
For example: Trying to read a non existing file from a program,problem Network connection , Operands being manipulated are out of prescribed ranges, class file missing,user entering invalid data.
For example: Trying to read a non existing file from a program,problem Network connection , Operands being manipulated are out of prescribed ranges, class file missing,user entering invalid data.
Difference between an error and exception:
Error:
Errors indicates serious problems which are usually not handled in our program.Error is irrecoverable
Example: memory error, hardware error, JVM error etc
Errors indicates serious problems which are usually not handled in our program.Error is irrecoverable
Example: memory error, hardware error, JVM error etc
Exception:
An exception is an abnormal condition in a java program which can be handled by the programmer and necessary steps can be taken in order to resume normal execution.Few examples of exceptions are
IOException
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
IOException
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
Common scenarios exceptions may occur:
To understand better I have listed few scenarios where exception may occur.
To understand better I have listed few scenarios where exception may occur.
- 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 gives 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
Why to handle exception?
If an exception is raised, which has not been handled by programmer then program execution can get terminated and system prints a non user friendly error message.
Comments
Post a Comment