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 {

 }
  • 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...) – Jon Skeet Nov 27 '14 at 10:34
  • 1
    After the exception is thrown there's nothing to do. That's why `finally` is there. Don't work there ;) – Maroun Nov 27 '14 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. – icbytes Nov 27 '14 at 10:38
  • put the statement 3 in catch too? now will you say that it's scope is only in try? – Sarthak Mittal Nov 27 '14 at 10:39
  • what if you put everything but the compulsory statement inside an inner try-catch? – Olavi Mustanoja Nov 27 '14 at 10:39
  • @JonSkeet This question was asked like this : Is there any way to do like this or not ??? Thilo - What the interviewer meant with scoping was We can't use the variables inside the Catch Block or Finally Block. They will lose scope after try{} block. – Randhish kumar Nov 27 '14 at 10:42
  • @SarthakMittal U got my question... Right, we don't have scope inside Catch{} block. I had also answered to write the statement 3 inside Catch{} block only. Do u think is there any other way ???? – Randhish kumar Nov 27 '14 at 10:44
  • @Randhishkumar let me try writing an answer! – Sarthak Mittal Nov 27 '14 at 10:47
  • @Randhishkumar you got what you were looking for? – Sarthak Mittal Nov 27 '14 at 10:59
  • Perhaps a trick question?? – Andre Lombaard Nov 27 '14 at 11:11

4 Answers4

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 {

}
Axel
  • 13,939
  • 5
  • 50
  • 79
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

Community
  • 1
  • 1
zapl
  • 63,179
  • 10
  • 123
  • 154
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.

RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24
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.

Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44