Try Catch Finally in Java
Exception An unwanted unexpected event that disturbs the normal flow of the program is called an exception. In java exceptions are handled using keywords try, catch. finally, throw and throws. The way it handled shown below.
try
{ // risky code
} catch (Exception e1) {
// catch block
}
finally
{
//cleanup code
}
Key points
- It is never recommended to take clean up code inside try block because there is no guarantee for the execution of every statement inside a try.
- It is never recommended to place clean up code inside the catch block because if there is no exception then catch block won't be executed.
- We require someplace to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether handled or not handled such type of place is nothing but finally block.
- Hence the main objective of finally block is to maintain cleanup code.
try
{ risky code
}
catch(x e)
{
handling code
}
finally
{
cleanup code
}
The specialty of finally block is it will be executed always irrespective of whether the exception raised or not raised and whether handled or not handled.
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("try block executed");
System.out.println(10/0);
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}}}
Output:
Try block executed
Catch block executed
Finally block executed
If return present in try or catch blocks first finally will be executed and after
that only return statement will be considered that finally blocks dominates return statement.
class ExceptionTest
{
public static void main(String[] args)
{
try
{
System.out.println("try block executed");
return;
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}}}
Output:
Try block executed
Finally block executed
If the return statement present try-catch and finally blocks
then finally block return statement will be considered.
class ExceptionTest
{
public static void main(String[] args)
{
System.out.println(methodOne());
}
public static int methodOne(){
try
{
System.out.println(10/0);
return 111;
}
catch(ArithmeticException e)
{
return 333;
}
finally{
return 151;
}}}
Output:
151
There is only one situation where the final block won't be executed is
whenever we are using System.exit(0) method.
Then JVM itself will be shut down , in this case finally block won't be executed.
i.e., System.exit(0); dominates finally block.
class ExceptionTest
{
public static void main(String[] args)
{
try
{
System.out.println("try");
System.exit(0);
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}}}
Output:
Try
If the Exception occurred in try-block is not handled in the catch block,
then the handling mechanism is followed. If finally, the block is present,
it will be executed followed by default handling mechanism.
class ExceptionTest
{
public static void main (String[] args)
{
try
{
int i = 10/0;
System.out.println("try");
}
catch(NullPointerException ex)
{
System.out.println("NullPointerException");
}
System.out.println("after try-catch");
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTest.main
Various possible combinations of try-catch-finally
1) Whenever we are writing try block compulsory we should write either catch or finally. i.e., try without a catch or finally is invalid.
2) Whenever we are writing catch block compulsory we should write a try. i.e., catch without try is invalid.
3) Whenever we are writing finally block compulsory we should write a try. i.e., finally without try is invalid.
4) In try-catch-finally order is important.
5) Within the try-catch-finally blocks, we can take try-catch-finally. i.e., nesting of try-catch-finally is possible. 6) For try-catch-finally blocks curly braces are mandatory.
class Test1 {
public static void main(String[] args){
try
{}
catch(ArithmeticException e)
{}
}}
Output:
Compile and running successfully.
public static void main(String[] args){ try {} catch(ArithmeticException e) {} catch(NullPointerException e) {} } } Output: Compile and running successfully.
class Test2 {
class Test3 {
public static void main(String[] args){
try
{}
catch(ArithmeticException e)
{}
catch(ArithmeticException e)
{}
}
}
Output:
Compile time error.
Test1.java:7: exception java.lang.ArithmeticException
has already been caught
catch(ArithmeticException e)
public static void main(String[] args){ try {} } } Output: Compile time error Test1.java:3: 'try' without 'catch' or 'finally' try
class Test4 {
class Test5 {
public static void main(String[] args){
catch(Exception e)
{}
}
}
Output:
Compile time error.
Test1.java:3: 'catch' without 'try'
catch(Exception e)
public static void main(String[] args){ try {} System.out.println("hello"); catch(Exception e) {} } } Output: Compile time error. Test1.java:3: 'try' without 'catch' or 'finally' Try
class Test6 {
public static void main(String[] args){ try {} catch(Exception e) {} finally {} } } Output: Compile and running successfully.
class Test7 {
public static void main(String[] args){ try {} finally {} } } Output: Compile and running successfully.
class Test8 {
public static void main(String[] args){ try {} finally {} finally {} } } Output: Compile time error. Test1.java:7: 'finally' without 'try' Finally
class Test9 {
public static void main(String[] args){ try {} catch(Exception e) {} System.out.println("hello"); finally {} } } Output: Compile time error. Test1.java:8: 'finally' without 'try' Finally
class Test10 {
public static void main(String[] args){ try {} finally {} catch(Exception e) {} } } Output: Compile time error. Test1.java:7: 'catch' without 'try' catch(Exception e)
class Test11 {
public static void main(String[] args){ finally {} } } Output: Test1.java:3: 'finally' without 'try' Finally
class Test12 {
class Test13 {
public static void main(String[] args){
try
{ try{}
catch(Exception e){}
}
catch(Exception e)
{}
}
}
Output:
Compile and running successfully.
public static void main(String[] args){ try { } catch(Exception e) { try{} finally{} } } } Output: Compile and running successfully.
class Test14 {
public static void main(String[] args){ try { } catch(Exception e) { try{} catch(Exception e){} } finally{ finally{} } } } Output: Compile time error. Test1.java:11: 'finally' without 'try' finally{}
class Test15 {
public static void main(String[] args){ finally{} try{ } catch(Exception e){} } } Output: Compile time error. Test1.java:3: 'finally' without 'try' finally{}
class Test16 {
public static void main(String[] args){ try{ } catch(Exception e){} finally { try{} catch(Exception e){} finally{} } } } Output: Compile and running successfully.
class Test17 {
When to use try-catch and throws in java?
How to handle exception in java?
How to get error code from exception in java?
What is try catch finally in java?
How to use try and catch in java?
Can we have multiple try blocks in java?
Can we use finally without try catch in java?
What is the use of finally block in java?
What will happen if an exception occurs in finally block in java?
That's it. In this article, we have seen the Try Catch Finally in Java and related cases in it.
0 Comments
Post a Comment