Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

nested try catch..

Thread view: 
graniteraju@gmail.com - 10 May 2006 20:14 GMT
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.
jmcgill - 10 May 2006 20:21 GMT
> 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.
graniteraju@gmail.com - 10 May 2006 20:55 GMT
my question is if i get an exception in the outer block will the
control still go to the inner block.
Stefan Schulz - 10 May 2006 21:44 GMT
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.
Oliver Wong - 10 May 2006 21:48 GMT
> 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
Juha Laiho - 16 May 2006 20:58 GMT
graniteraju@gmail.com 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.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.