Nested try block:
try block within a try block is known as nested try block.
Why use nested try block used and where ?
Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error.
In such cases, exception handlers have to used nested try block.
Syntax:
......
try
{
statement 1;
statement 2;
try
{
statement 1;
statement 2;
}
catch(Exception e)
{
}
}
catch(Exception e)
{
}
......
For Example:
class Test{
public static void main(String []args){
try{
try{
System.out.println("going to divide by zero");
int x =25/0;
}
catch(ArithmeticException ex) {
System.out.println(ex);
}
try{
int y[]=new int[10];
y[12]=8;
}
catch(ArrayIndexOutOfBoundsException ex){
System.out.println(ex);
}
System.out.println("other statement");
} // end of outer try bock
catch(Exception ex){
System.out.println("Exception handled");
} // end of outer catch bock
System.out.println("normal flow");
}
}
Output: going to divide by zero
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: 12
other statement
normal flow
finally block:
The finally block is a block that is always executed.It is mainly used to perform some important tasks such as closing connection, file,stream,transaction etc.
Note:Before terminating the program, JVM executes finally block(if any).
Note:finally must be followed by either try or catch block.
Why use finally block ?
finally block can be used to put "cleanup" code such as closing a file,closing connection etc.
Examples of the finally block
>1. Program in case exception does not occur
class Test{
public static void main(String [])args{
try{
int a=40/4;
System.out.println(a);
}
catch(NullPointerException ex){
System.out.println(ex);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code");
}
}
Output: 10
finally block is always executed
rest of the code
>2. Program in case exception occured but not handled
class Test{
public static void main(String []args){
try{
int a=15/0;
System.out.println(a);
}
catch(NullPointerException e){
System.out.println(e);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code");
}
}
Output: finally block is always executed
Exception in thread main java.lang.ArithmeticException:/ by zero
>3. Program in case exception occured and handled
class Test{
public static void main(String []args){
try{
int data=15/0;
System.out.println(a);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally { System.out.println("finally block is always executed");
}
System.out.println("rest of the code");
}
}
Output: Exception in thread main java.lang.ArithmeticException:/ by zero
finally block is always executed
rest of the code
Note: For each try block there must be zero or more catch blocks, but only one finally block.
Note: The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
Next>> What is throw and throws keyword in Java
try block within a try block is known as nested try block.
Why use nested try block used and where ?
Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error.
In such cases, exception handlers have to used nested try block.
Syntax:
......
try
{
statement 1;
statement 2;
try
{
statement 1;
statement 2;
}
catch(Exception e)
{
}
}
catch(Exception e)
{
}
......
For Example:
class Test{
public static void main(String []args){
try{
try{
System.out.println("going to divide by zero");
int x =25/0;
}
catch(ArithmeticException ex) {
System.out.println(ex);
}
try{
int y[]=new int[10];
y[12]=8;
}
catch(ArrayIndexOutOfBoundsException ex){
System.out.println(ex);
}
System.out.println("other statement");
} // end of outer try bock
catch(Exception ex){
System.out.println("Exception handled");
} // end of outer catch bock
System.out.println("normal flow");
}
}
Output: going to divide by zero
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: 12
other statement
normal flow
finally block:
The finally block is a block that is always executed.It is mainly used to perform some important tasks such as closing connection, file,stream,transaction etc.
Note:Before terminating the program, JVM executes finally block(if any).
Note:finally must be followed by either try or catch block.
Why use finally block ?
finally block can be used to put "cleanup" code such as closing a file,closing connection etc.
Examples of the finally block
>1. Program in case exception does not occur
class Test{
public static void main(String [])args{
try{
int a=40/4;
System.out.println(a);
}
catch(NullPointerException ex){
System.out.println(ex);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code");
}
}
Output: 10
finally block is always executed
rest of the code
>2. Program in case exception occured but not handled
class Test{
public static void main(String []args){
try{
int a=15/0;
System.out.println(a);
}
catch(NullPointerException e){
System.out.println(e);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code");
}
}
Output: finally block is always executed
Exception in thread main java.lang.ArithmeticException:/ by zero
>3. Program in case exception occured and handled
class Test{
public static void main(String []args){
try{
int data=15/0;
System.out.println(a);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally { System.out.println("finally block is always executed");
}
System.out.println("rest of the code");
}
}
Output: Exception in thread main java.lang.ArithmeticException:/ by zero
finally block is always executed
rest of the code
Note: For each try block there must be zero or more catch blocks, but only one finally block.
Note: The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
Next>> What is throw and throws keyword in Java
No comments:
Post a Comment