yes ,we can stop execution of finally block but condition is that control must entered into the finally block
finally block is executed only when control entered into try block so we need write code in try block to stop execution of finally block
//Simple example to Demonstrate above concept without stoping execution of finally block:
public class MainAppp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println("IN THE TRY BLOCK");
}
finally
{
System.out.println("IN THE FINALLY BLOCK");
}
}
}
O/P
finally block is executed only when control entered into try block so we need write code in try block to stop execution of finally block
//Simple example to Demonstrate above concept without stoping execution of finally block:
public class MainAppp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println("IN THE TRY BLOCK");
}
finally
{
System.out.println("IN THE FINALLY BLOCK");
}
}
}
O/P
IN THE TRY BLOCK
IN THE FINALLY BLOCK
//Simple Example To Stop Finally block Execution
public class MainAppp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
System.out.println("IN THE TRY BLOCK");
System.exit(0);
}
finally
{
System.out.println("IN THE FINALLY BLOCK");
}
}
}
O/P:
IN THE TRY BLOCK
Explanation
System.exit(0):
0--- indicate status code it means normal termination
nonzero--indicate abnormal termination.
No comments:
Post a Comment