aspose file tools*
The moose likes Beginning Java and the fly likes Nested try/catch blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested try/catch blocks" Watch "Nested try/catch blocks" New topic
Author

Nested try/catch blocks

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi,
I wonder if anyone can help. Basically, my question is are nested try / catch blocks valid / efficient / the best way to do things ?

Here's what I mean - I have a bit of code where I'm calling many methods that throw exceptions that I need to catch. So far, I've written the try / catch in a nested way - but it means that my code is massively indented, and the more code I add, the worse it gets !
For example:


I've tried writing the code in a linear fashion:


But that does not compile as s is not defined within the try/catch block of the s.getDatabase. I understand that, no problems.

So, back to the orginal question - are nested try/catch blocks the way to go ? or is there a better way ?

Cheers

Nathan
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Nested exceptions IMHO are always bad - you get ugly, hard to read code. In your example, you catch the same exceptions twice. Why? The problem (to the rest of you app anyway) is the same - you can't acces the DB. Why not just wrap all that code in a try/catch block and log the exceptions?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Nested exceptions is not necessarily bad. It is like lots of other tools, if used correctly they can help you easily build some complicated stuff, but when in the hands of evil, they can bring an app down, and make it impossible to read and maintain.

Here's my look.

You would use nested Exceptions, and sometimes catching the same exception, if you want the outer code to continue running when an exception occurs in the inner code.

Second, I would also pull out the inner code into a seperate method that the outer code can call. This will stop the massive indentation, and achieve the same goals. Actually making the code more reusable and maintainable.

Here's an Example. I am actually going to make it two seperate posts, for the ability to compare. I am going to use some psuedocode.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12


But that does not compile as s is not defined within the try/catch block of the s.getDatabase. I understand that, no problems.
[ March 14, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

OK Here's the code that has it all on one level. Notice how any exception will cause the catch to run and no more code will run in the try code.



Now on to the other example.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Now here is where I seperate into two seperate methods, the inner handles the same exceptions in my example.




I hope those two examples demonstrate how nested try statements can lead to some powerful code, if done correctly. And it always depends on the requirements of the app/code/task.

Mark
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Thats what I love about this forum - people respond real quick ! Thanks !

OK, Paul:
Nested exceptions IMHO are always bad - you get ugly, hard to read code.

Exactly what I'm trying to prevent - I agree, it is ugly and hard to read.
In your example, you catch the same exceptions twice. Why?

OK, so not the best example (but it is a real example). But equally, it could be calling methods that throw different exceptions - how would you deal with that ?

Mark - I like the sound of what you're saying - I look forward to reading your examples.

Marilyn - I very much like your solution - thats pretty neat !

Thanks to all - look forward to reading more comment on this subject !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested try/catch blocks