• Post Reply Bookmark Topic Watch Topic
  • New Topic

should I put every thing inside a try catch block?  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in a medium sized web/ejb based proyect it is convenient to use a lot of try catch blocks, the objective is avoid those exception pages and allow the user to keep going. it consumes more resources?
 
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jorge,

I'm not sure if I understand your question correctly. But in general it's surely not a good solution to put everything in a try-catch block. Surely exception handling can be tricky sometimes but exceptions are not only thrown to annoy the developer. There's always a good reason why exceptions are thrown and you should take time to think about each one carefully. You don't gain anything if you put every statement inside a big try-catch block and maybe even ignore thrown exceptions. Instead you should handle exceptions because they are a sign for exceptional conditions. Only handling them correctly allows a user to proceed. If you ignore this fact your system will probably be in an inconsistent state which is definitely not what you or your users want.

So it's NOT convenient to use try-catch but instead it's necessary to use try-catch where appropriate and deal with each exceptional situation as good as you can! Of course it's not easy to give general advices what is the best way "to deal" with an exception.

Marco
 
Jorge Bendahan
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Marco, so where is a good place to put the try catch? a type casting? if, for statements?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general there is one or a few statements inside your could which could potentially throw exception! Usually you wrap these statement(s) inside a try-catch block. It doesn't make sense to make the scope of this block broader than necessary because it should be obvious why there is a try-catch and what you plan to do to handle an exception raised from these statements.

As I already said exception handling is not always as easy as it may seam and opinions about good exception handling may differ. In Java there's additionally a special problem because of the difference between checked and unchecked exceptions. This often leads to even more confusion.

A good rule-of-thumb is to only catch exceptions which you can reasonably handle in this place you catch them. "Handle" does NOT mean that you should just log them and throw them away thereafter. You should have a way to handle the exceptional condition so that the application is in a well-defined state again and can progress its work. What this means in practice is very specific to the application context. For example if a user gives a name for a file to open and this file doesn't exist an exception may be thrown. Just ignoring the exception doesn't gain you anything. Instead you could tell the user, that there was no file found and let him try again. But it's hard to explain every situation which could lead to exceptions in this forum

There are other useful rules you should follow. Do yourself a favor and have a look at different tutorials and articles about this topic. Although there are also a lot of examples which aren't really good, this should give you a feeling what good exception handling could look like. As I said it's impossible to explain this here in short. But it would be easier if you come back with some more concrete questions for specific situations

Marco
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!