Exception Handling:
The exception handling is one of the powerful mechanism provided in java.It provides the mechanism to handle the runtime errors so that normal flow of the application can be maintained.
The Dictionary Meaning of Exception is an 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.
Note: Exception Handling is a mechanism to handle runtime exceptions and errors.
Advantage of Exception Handling:
The main advantage of exception handling is that normal flow of the application is maintained Exception normally disrupts the normal flow of the application that is why we use exception handling.
Suppose there are 20 lines in your program and there occurs an exception at line 8, rest of the code will not be excecuted i.e. statement 9 to 20 will not run.If we perform exception handling, rest of the exception will be executed.That is why we use exception handling.
Types of Exception:
There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception.
The according to sun microsystem there are three types of exceptions in java:
1. Checked Exception
2. Unchecked Exception
3. Error
Note: Throwable class is the super class of Exception.
What is the difference between checked and unchecked exceptions ?
>1.Checked Exception:
The classes that extend Throwable class except RuntimeException class and Error are known as checked exceptions e.g.IOException, ServletException, FileNotFoundException, SQLException etc. Checked exceptions are checked at compile-time.
>2.Unchecked Exception
The classes that extend RuntimeException class are known as unchecked exceptions e.g. ArithmeticException,NumberFormatException,NullPointerException,ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
>3.Error
Error is an irrecoverable(that can not be recoverable) e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Common scenarios of Exception Handling where exceptions may occur:
There are given some scenarios where unchecked exceptions can occur. They are given below:
1. ArithmeticException occurs scenarios :
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0; //ArithmeticException
2. NullPointerException occurs scenarios
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s1=null;
System.out.println(s1.length()); //NullPointerException
3. NumberFormatException occurs scenarios
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 s1="xyz";
int a=Integer.parseInt(s1); //NumberFormatException
4. ArrayIndexOutOfBoundsException occurs scenarios
If you are inserting any value at wrong index position in Array, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[10];
a[12]=20; //ArrayIndexOutOfBoundsException
Next>> Exception Handling using try and catch
The exception handling is one of the powerful mechanism provided in java.It provides the mechanism to handle the runtime errors so that normal flow of the application can be maintained.
The Dictionary Meaning of Exception is an 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.
Note: Exception Handling is a mechanism to handle runtime exceptions and errors.
Advantage of Exception Handling:
The main advantage of exception handling is that normal flow of the application is maintained Exception normally disrupts the normal flow of the application that is why we use exception handling.
Suppose there are 20 lines in your program and there occurs an exception at line 8, rest of the code will not be excecuted i.e. statement 9 to 20 will not run.If we perform exception handling, rest of the exception will be executed.That is why we use exception handling.
Types of Exception:
There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception.
The according to sun microsystem there are three types of exceptions in java:
1. Checked Exception
2. Unchecked Exception
3. Error
Note: Throwable class is the super class of Exception.
What is the difference between checked and unchecked exceptions ?
>1.Checked Exception:
The classes that extend Throwable class except RuntimeException class and Error are known as checked exceptions e.g.IOException, ServletException, FileNotFoundException, SQLException etc. Checked exceptions are checked at compile-time.
>2.Unchecked Exception
The classes that extend RuntimeException class are known as unchecked exceptions e.g. ArithmeticException,NumberFormatException,NullPointerException,ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
>3.Error
Error is an irrecoverable(that can not be recoverable) e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Common scenarios of Exception Handling where exceptions may occur:
There are given some scenarios where unchecked exceptions can occur. They are given below:
1. ArithmeticException occurs scenarios :
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0; //ArithmeticException
2. NullPointerException occurs scenarios
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.
String s1=null;
System.out.println(s1.length()); //NullPointerException
3. NumberFormatException occurs scenarios
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 s1="xyz";
int a=Integer.parseInt(s1); //NumberFormatException
4. ArrayIndexOutOfBoundsException occurs scenarios
If you are inserting any value at wrong index position in Array, it would result ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[10];
a[12]=20; //ArrayIndexOutOfBoundsException
Next>> Exception Handling using try and catch
No comments:
Post a Comment