How to skip Finally Block

#1

class excep
{
public static void main(string args[]) {
try
{
some code;
}
finally
{
some caught exception;
}
}
}

I want to skip the finally block even though any exception occur in try block.
how to handle this.




Thanks in advance,

ramsri4ever.
 
#3
Hi dude,

Thanks for your reply.

Kindly try this below code:

class excep
{
public static void main(String[] args)
{
try
{
int i=1/0;
System.out.println("in try block:::" +i);
System.exit(0);
}
finally
{
System.out.println("In finally block:::");
}
}
}

Finally block not skipped when i use system.exit(0) in try block.

When i use System.exit(0) in catch block the finally block get skipped.

I want to skip finally block when i use try and finally block alone.

Note:I have not mentioned catch block.

Whether its possible to do.

Ramsri4ever
 
#4
Hello Mr. Ramshree4ever

if you only want try and finally block and want finally to be skipped
then you must have to write system.exit(0) before the exception occur in the try block.

in the situation u have mentioned the system.exit(0) is not reachable code as the exception have occured before it so the control have jumped out to try block.
 
#5
i think you don't have knowledge in java


finally method using only for either exception is occurred or not
but finally() executed

it is using is jdbc coneection close
 
#7
import java.io.*;
class Sample
{
public static void main(String args[])
{
BufferedReader br=null;
try
{
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(br.readLine());
br.close();
System.exit(0);
}
catch(Exception e)
{
System.err.println(" Fatal Error:"+e.getMessage());
}
 
#8
Hi,

Please try this....

Here if we want to skip finally block we have to add catch block and with in that catch block we have to write System.exit(0), so if exception occurs the control will come out from program
 
#9

class excep
{
public static void main(string args[]) {
try
{
some code;
}
finally
{
some caught exception;
}
}
}

I want to skip the finally block even though any exception occur in try block.
how to handle this.




Thanks in advance,

ramsri4ever.



purpose of Finally Block:
if you want to print anything definitely on your output device irrespective of your program logic, then it is recommended you to use finally.


Answer for your Question:
You have no need to write caught exceptions on finally block. for every try block you must have to write a catch block to handle the exceptions.
check this out for your required output....


Have a good time..
 
#10
Here what i am saying is we have to write catch block after try block(Not with in the finally block).. so that if any exception occurs with in the try block the control will jump from try to catch. With in this catch it will execute System.exit(0), so that immediatly after completion of execution of this line the control will come out from the program instead of going to finally block.



class excep
{
public static void main(string args[]) {
try
{
some code;
}
catch(Exception e)
{
System.exit(0);
}


finally
{
some caught exception;
}
}
}
 
#11
hi ramsri
try to this code

class Test {
public static void main(String args[]) {
try {
System.out.println("this is starting try");
int a=10/0;
}
final {
System.exit(0);
System.out.println("this statement is not printed");
}
}
}

but System.exit(0) is not safe manner,its terminate entire application.

- final block is used for closing connection etc...in any application closing connection is very importent otherwise that data is loosing.

i think it is useful for you..

regarding
-hari
 
#13
Assuming that a special case is brought up On attempt block, control passes with make square whether it exists Overall to At last piece. At long last square may be constantly executed At an special case happens and the best route will dodge execution about whatever proclamations to At long last square is by aborting the code forcibly by composing accompanying accordance for code toward those wind from claiming attempt square.
 
Top