Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested try/catch blocks

 
Nathan Russell
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic