Java have very strong feature of exception handling. Exception is the problem which might occur during exeuction of program and we are working on advance to handle those problem by handling exception. Problem can occur due to any of the below situation
1. User provided wrong input
2. ALL IO operation based on disk available, folders, permission ….etc. We are searching for file and it is not available. We are trying to write to a file but permission is not there program to write.
3. Error: These are unhanded situation where JVM itself have got issue to resolve.
2. ALL IO operation based on disk available, folders, permission ….etc. We are searching for file and it is not available. We are trying to write to a file but permission is not there program to write.
3. Error: These are unhanded situation where JVM itself have got issue to resolve.
Basically, we have broadly two types problems to encounter
1. Exception : Exception can be caught and handled. We can write program to handle those situation.
2. Error: Those are JVM error and can not be handled. Program will be terminated.
2. Error: Those are JVM error and can not be handled. Program will be terminated.
Exception Hierarchy : Hierarchy starts with Throwable class and as we know that all classes extends by default Object class. It has two child classes Error and Exception. Exception is top class for all exception related classes. Any kind of exception can be caught with Exception class.
Throwable class provides multiple method geMessage(), getCause(), GetPrintStackTrace() to help in exception handling.
Simple Program of exception handling :
class ExceptionExample { public static void main(String ars[]) { int arr[]={12,14}; try { System.out.println(arr[3]); } catch (ArrayIndexOutOfBoundsException e) {System.out.println(e.toString());} System.out.println("Caught exception and program is still continuing"); } }
Output of single exception handling program :
G:\personal>java ExceptionExample java.lang.ArrayIndexOutOfBoundsException: 3 We are able to catch exception and program is continue
Now, we will discuss following points as given below :
1. Try with catch block
2. Try without catch block and with finally block
3. try with catch and finally block
4. Try with multiple catch block
5. Throw and Throws concept with user defined exception
1. Try with catch block
2. Try without catch block and with finally block
3. try with catch and finally block
4. Try with multiple catch block
5. Throw and Throws concept with user defined exception
1. Try with Catch block in Java: Above example is try with one catch block.
2. Try without catch block and with finally block : Now look below example carefully It has try block but there is no catch block only finally block. Compiler will allow program to compile try either with catch or finally block but program will crash after printing line in finally.
2. Try without catch block and with finally block : Now look below example carefully It has try block but there is no catch block only finally block. Compiler will allow program to compile try either with catch or finally block but program will crash after printing line in finally.
class ExceptionExample { public static void main(String ars[]) { int arr[]={12,14}; try { System.out.println(arr[3]); } finally { System.out.println("We are able to catch exception and program is continue"); } } }
Output of the program :
G:\personal>java ExceptionExample We are able to catch exception and program is continue Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at ExceptionExample.main(ExceptionExample.java:8)
4. Try with multiple catch block We can have multiple exception in one try block as it is given
try
{
//attempted code
}
catch(ArrayIndexOutOfBoundsException e){ //write code to print}
catch(Exception e){//write code to print}
{
//attempted code
}
catch(ArrayIndexOutOfBoundsException e){ //write code to print}
catch(Exception e){//write code to print}
There is two points very important to remember
A) Finally block always executed in exception handling of java
B) Try block can be nested as given below
try{
//some code
try{
//some code
}catch(Exception e1){}
}catch(Exception e{}
C) If multiple catch blocks need to be written for one try block then we can write from subclass to parent class. Exception class will catch all kind of exception. It is better to catch exact exception with exact class in spite of using super class Exception.
3. try with catch and finally block : It is best to write try with catch and finally. For example, in jdbc program, you can catch exception of classnotfoundexception and sqlexception and close all connection in finally block.
A) Finally block always executed in exception handling of java
B) Try block can be nested as given below
try{
//some code
try{
//some code
}catch(Exception e1){}
}catch(Exception e{}
C) If multiple catch blocks need to be written for one try block then we can write from subclass to parent class. Exception class will catch all kind of exception. It is better to catch exact exception with exact class in spite of using super class Exception.
3. try with catch and finally block : It is best to write try with catch and finally. For example, in jdbc program, you can catch exception of classnotfoundexception and sqlexception and close all connection in finally block.
class ExceptionExample { public static void main(String ars[]) { int arr[]={12,14}; try { System.out.println(arr[3]); } catch (ArrayIndexOutOfBoundsException e) {System.out.println(e.toString());} finally { System.out.println("We are able to catch exception and program is continue"); } } }
4. Try with multiple catch block
One try can have multiple catch block and we can catch multiple exception. Syntax are given below
try
{
//code here
}
catch(OneTypeOFException e){}
catch(TwoTypeOFException e){}
catch(ThreeTypeOFException e){}
One try can have multiple catch block and we can catch multiple exception. Syntax are given below
try
{
//code here
}
catch(OneTypeOFException e){}
catch(TwoTypeOFException e){}
catch(ThreeTypeOFException e){}
5. Throw and Throws concept with user defined exception
We have two words in java. One is throw and another one is throws. Throw is used to create your own exception. This is also called user created exception. We use throws keyword with method. It means that we do not catch exception and declare in methods as throws. Code is used this particular methods with throws exception need to catch it or use it throws keyword again.
Below is given example of creating new user defined exception with throw keyword and declared along with throws in method declaration.
class ThrowExample { public void helloMethod() throws Exception { throw new Exception("Hello Method exception"); } } class Test { public static void main(String args[]) { ThrowExample teObj = new ThrowExample(); try { teObj.helloMethod(); }catch(Exception e){System.out.println(e.toString());} } }
Output of the throw, throws and user defined exception example in java :
G:\>java Test java.lang.Exception: Hello Method exception
0 comments:
Post a Comment