nested try catch..


G

graniteraju

I have nested try catch loops.If an exception is raised in the outer
try will it return from the method after being caught in th eouter
catch or will it go back to the inner try again after being caught in
the outer catch.
 
Ad

Advertisements

J

jmcgill

I have nested try catch loops.If an exception is raised in the outer
try will it return from the method after being caught in th eouter
catch or will it go back to the inner try again after being caught in
the outer catch.

Are you asking if that's a good design? (It can be very good and
useful, and it makes it very clear to a maintainer that there are two r
more levels of error handling.)

But the way it works, is that an exception thrown in the inner block may
be caught by the inner catch, if the type matches. That inner catch can
re-throw the exception or not. In either case, the inner block is gone
out of scope completely, and control transfers to the outer block. If
the inner exception is not caught by the inner block, the inner block's
finally still runs but the exception is still going to interrupt the
outer block in that case.

I'm doing a poor job of explaining.

Think of what happens when one method calls another and each method has
try-catch-throw-finally blocks. The nested behavior is really a lot
like anonymous methods.
 
G

graniteraju

my question is if i get an exception in the outer block will the
control still go to the inner block.
 
S

Stefan Schulz

Show us the code, what exactly do you mean.

My guess is you have a construction like this:

try {
try {
// ...
} catch (Exception1 e) {
// ...
}
} catch (Exception 2 e2){
// ...
}

Now, why should your execution ever return to the inner block? It is
"broken out" even if catch (Exception1 e) would match the exception
actually thrown.
 
O

Oliver Wong

my question is if i get an exception in the outer block will the
control still go to the inner block.

Using a slightly modified version of Schulz's code:

try {
try {
A()
} catch (Exception1 e) {
B()
}
} catch (Exception2 e){
C()
}

Assume that Exception1 and Exception2 don't inherit from each other.

If A() throws Exception1, B() will get executed.
If A() throws Exception2, C() will get executed, and B() *WON'T* get
executed.
If A() throws Exception3 (which isn't caught here), it'll go up the call
stack.
If A() throws no exception, neither B() nor C() will execute.

If B() throws Exception1, it will will go up the call stack.
If B() throws Exception2, C() will execute.

If C() throws any exception, it'll go up the call stack.

- Oliver
 
Ad

Advertisements

J

Juha Laiho

(e-mail address removed) said:
I have nested try catch loops.

I think the above sentence is where your thinking goes muddy.
A try-catch structure is not loop. You'll never go "backwards" with
try-catch [-finally].
If an exception is raised in the outer try will it return from the
method after being caught in th eouter catch or will it go back to
the inner try again after being caught in the outer catch.

No; when an exception is thrown within a "try" block, the control will
be passed to the corresponding "catch" block. If the type of exception
thrown matches the type of exception for the "catch", then the catch
block will be executed. Otherwise, the exception will be passed to the
"catch" block corresponding to the enclosing "try" block, and handled
in a similar fashion.

After the exception has been handled in some "catch" block, the execution
will continue from the end of that "catch" block. If there was no matching
"catch" block, the thread will terminate.

"finally" blocks will be executed whenever execution exists a "try" block.
If there was a "catch" block matching the exception, the "finally" block
will be executed after the "catch" block.

But as said, your main issue seems to be to consider try-catch a form of
loop, which it is not. Drop that, and you'll see that the execution flow
is natural.
 
Ad

Advertisements


Top