• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

A recommended use of try / catch

What's the recommended use of try / catch? I currently use it for operations I know can throw an exception (like connecting to a database). But even something as simple as string operation can throw an exception, although it's not expected. So, should the main function enclose everything in the try / catch block? If yes, should I still have smaller enclosures with specialized exception catching?

Thanks
0
gromul
Asked:
gromul
  • 2
  • 2
  • 2
2 Solutions
 
dstanley9Commented:
Here are a few opinions:

http://blogs.msdn.com/ericgu/archive/2006/02/15/532615.aspx
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=128

In general, you want to catch specific exceptions where you know something could go wrong and you can handle it appropriately. (parsing, dividebyzero, SQL connections, etc., etc.) but let general exceptions bubble up to a top-level exception handler, where you can do logging and notifications.

Think of it this way:  if you can catch the excpetion, what are you going to do with it?  Log it?  ignore it and move on?  

0
 
mrichmonCommented:
In general you should catch specific exceptions you know may happen.  They should be of specific types.

You should try to aviod catching the System.Exception general type.

If you can test something first and by doing so avoid the try/catch you will have a performance gain.

In general you most likely want a logging mechanism for all exceptions.

If you can recover, log then recover.
If you can't then log and DO NOT CATCH.

However, also consider having a global exception handler for websites or applications that basically log the exception then display a friendly error mesage.

Some more articles:
Exception Handling Best Practices in .NET
http://www.codeproject.com/dotnet/exceptionbestpractices.asp

Best Practices: Exception Management
http://dotnetjunkies.com/Article/197E493F-BA73-45A2-B39A-4EA282A2E562.dcik

Best Practices of Exception Management
http://www.c-sharpcorner.com/Code/2005/March/ExceptionManagement.asp
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gromulAuthor Commented:
mrichmon, what do you mean by this: "If you can't [recover] then log and DO NOT CATCH"? Have just a try block, then catch it somewhere else? But where do I log then?
0
 
mrichmonCommented:
Well basically somehing like this

try
{
  code goes here
}
catch (yourexceptiontype ex)
{
  // first log either way

  // now determine if you can recover or not
  if(canRecover)
  {
       recover code
    }
   else
    {
         throw;
   }

So you attempted to catch, but you re-threw the error.

Or if you know that there is no chance of recovery of the application for certain things, then instead don't even put that code in a catch, and allow your global handler to catch, log, and then redirect to a friendly error page.
0
 
gromulAuthor Commented:
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now