0

Without using finally, how can we execute any compulsory statement even after exception is thrown ?? Furthermore, the variables used or the method has scope only inside the try block. This question was asked by me in a interview. please suggest the answer.

try{ //........ statement 1 // ....... statement 2 might throw an Exception // ....... statement 3 - A compulsory statement needs to be executed even if exception is thrown. } catch { } 
10
  • 9
    That sounds like a bizarre question to me - and one which would make me think twice about wanting to join the company involved. (This is precisely what finally is for...) Commented Nov 27, 2014 at 10:34
  • 1
    After the exception is thrown there's nothing to do. That's why finally is there. Don't work there ;) Commented Nov 27, 2014 at 10:34
  • No. Not exklusive. If you write lines after a local catch, which does not directly propagates the exception upwards to the caller, each line after the catch block gets executed. Commented Nov 27, 2014 at 10:38
  • put the statement 3 in catch too? now will you say that it's scope is only in try? Commented Nov 27, 2014 at 10:39
  • what if you put everything but the compulsory statement inside an inner try-catch? Commented Nov 27, 2014 at 10:39

4 Answers 4

2

This is really only academic - if you want a statement to be executed after an exception is thrown, you really should use finally. However, you could catch the exception in a try-catch-block, put your statement inside the catch clause, and then rethrow the exception. Emphasis on could, of course you should not.

/* * DO NOT DO THIS! (Even if you could.) */ try { //........ statement 1 Exception e = null; try { // ....... statement 2 might throw an Exception } catch (Exception e2) { e = e2; } // ....... statement 3 - A compulsory statement // needs to be executed even if exception is thrown. if (e!=null) { throw e; } } catch { } 
Sign up to request clarification or add additional context in comments.

Comments

0

You could be wrapping the problematic part into another catch block and manually manage what finally would do

try{ //........ statement 1 Exception saved = null; try { // ....... statement 2 might throw an Exception } catch (Exception e) { saved = e; } // ....... statement 3 - A compulsory statement // needs to be executed even if exception is thrown. if (saved != null) throw saved; } catch { } 

That's kind of problematic though since you'd have to catch(Throwable) to get the same effect as finally, and Throwable is a checked exception meaning that you suddenly have to declare it or use dirty tricks: Java SneakyThrow of exceptions, type erasure

Comments

0

What about this:

boolean flag = true; int firstExecution = 0; while(flag){ try{ firstExecution++; if(firstExecution==1){ //........ statement 1 // ....... statement 2 might throw an Exception } // ....... statement 3 - A compulsory statement needs to be executed even if exception is thrown. flag=false; } catch {} } 

And if you want to try to execute statement 2 again and again then you can shift the bracket below statement 2 to above it.

2 Comments

nice but kind of overly complicated
thanks, yeah it's a bit complicated but if user wants to try to execute statement 2 again and again then this would be a simple option :)
0

VERY BAD EXAMPLE NEVER DO THIS. USE FINALLY. JAVA DEVELOPERS HAVE DONE SO MUCH TO PROVIDE YOU WITH FINALLY.

try {

// statement 1 try { // statement 2 ---- might throw an Exception } catch (Exception e) { } // Put your compulsory statement 3 here. 

} catch {

}

This is what i can suggest.

2 Comments

I had also answered the same. But he was not satisfied with my answer the the variables would lose the scope after try {} block ( according to my INterviewer)
So does he mean that all three statement be in the same scope ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.