views:

894

answers:

15

Hi folks,

I've started using try catch blocks (bit late i know!) but now im not sure what to do with the exception once i've caught it. What do people usually do here?

-- Jonesy

     Try
        connection.Open()

        Dim sqlCmd As New SqlCommand("do some SQL", connection)

        Dim sqlDa As New SqlDataAdapter(sqlCmd)

        sqlDa.Fill(dt)

    Catch ex As SQLException
              ' ahhhh what to do now!!!?
    Finally
        connection.Close()
    End Try
+16  A: 

What do you want it to do? It depends entirely on your application.

You could have it retry, you could display a messagebox to the screen, write to a log, the possibilities are endless.

As a developer you can't predict every possible error. It's a good idea to have generic catch code in even if you don't know how to fix the error. You could be saving to a database, and one of 50 database errors might occur, and you won't be able to write code to handle every single one.

You should put a generic catch in to make sure your program doesn't simply crash, and in some cases simply displaying the error and letting them have another go is enough. Imagine if the cause was 'disk is full'. You could merely print out the exception, and let the user try again - they'd know what to do, and solve the problem themselves.

This is why I disagree with the comment about not handling it if you don't know what to do. You should always handle it, even if it is just to display a messagebox saying 'Error', because it's infinitely better than "This program has performed an illegal operation and will be closed."

SLC
I would also recommend reducing the amount of code inside your block, especially in the above example. You are a) connecting to a database and b) executing an SQL command, both of which can raise a lot of exceptions. Put each in a separate try catch (or parse your ex/exception) so you have a better idea of what type of error you're dealing with (have you been punched in the eye or been stamped on the foot?)
CResults
You could also end end the application as the exception is fatal
Victor Hurdugaci
@Victor Hurdugaci that exception is not fatal, unless the program requires that data to be running. I always try to not end the program because normally you can let the user try again, try again, let them know something is wrong, let the user know what to do. I don't like programs I've been using to just shutdown.
msarchet
"You should always handle [exceptions]". Wow, is that wrong. Epic wrong. Super duper wrong. Cite me one big name who agrees with you on this. This is why the AppDomain.UnhandledException event exists. You can handle this event and show your users a nice "You're F-ed" dialog. You don't have to slop up your UI-level code with catches around everything. Sheesh.
Will
Can you explain why it's wrong? I don't quite understand your comment. Nothing could be worse than your program crashing with a generic windows error.
SLC
How do you decide which segments of code should require a try catch? (and avoiding using the block around everything...)
baron
@SLC Will means that you can prevent your program crashing by adding **one** single general exception handler: you don't need to put exception handling in **every single routine**. Check out the documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.application.setunhandledexceptionmode.aspx
MarkJ
@Baron You can pretty much tell, some code that puts text into a textbox isn't going to break, but code that connects to a database and updates a record, or writes to a logfile, etc. is likely to be prone to breaking. You could have global error catching code, but I can't see how that would be a good thing, especially if some errors can be ignored with just a warning dialog ("Log not written: disk full"). There is no global solution to every error and so relying on catching the exception globally is not a good solution, but more of a failsafe. You should take care of it before it comes to that
SLC
A: 

I would consider logging that the exception took place and notify the user that you were unable to complete their request. This assumes that this SQL request is necessary to completion of the action the user is attempting to perform.

Adam Driscoll
A: 

It depends entirely on context. Possible options include "Use default data instead of data from the database" and "Display an error message to the user".

David Dorward
A: 

If you don't want to do anything with it, you can always just do try/finally rather then try/catch/finally

Matt Briggs
+8  A: 

It depends on what the sql does really. Is it something:

  • that the user did ? Perhaps create an account or a message - then you need to inform the user that something is wrong
  • that the application does naturally ? Like cleaning up logs or something similar? Is it fatal? Can the application go on? Is it something that can be ignored - perhaps retried? Should the administrator be announced?

Start with these questions, they will usually lead to answers about how to treat exceptions

laura
+3  A: 

Unless you were expecting the error (the API says it can occurr) and can handle it (there is an obvious step to take if the exception occurrs), you probably want to crash with a lot of noise. This should happen during testing / debugging, so that the bug can get fixed before the user sees it!

Of course, as commenters have pointed out, the user should never really see all this noise. A high-level try/catch or some other method (EMLAH, I hear for .net web projects) can be used to show the user a nice error message ("Oops! Something went wrong! What on earth were you trying to do?") with a submit button...

So basically, my point is this: Do not catch errors you don't know how to handle! (except for the high-level handler). Crash early and with as much information as possible. This means you should log the call stack and other vital information if at all possible for debugging.

Daren Thomas
On the other hand, never ever "crash with a lot of noise" before user eyes. Say something like "Ops, something went wrong, we're working on it.". No details to see in public.
Developer Art
Best to display a custom page with a "please tell us what you were doing" textbox and submit button, so they can vent.Of course, don't hook it up to anything.
SLC
A: 

If don't know how to react to it, don't catch the exception.

Catch it instead in a place where you have the possibility to handle it and where you know how to continue execution afterwards.

sth
A: 

1st if there is no way for your application to handle the exception then you shouldn't catch it in the first place (logging can be done via exception event handlers without catching )

if there is something you can do then do that. e.g. roll back transaction and report it failed to user. this is going to depend on your application

jk
+7  A: 

depends on your business logic but you may want to do one or more of the following:

  • rollback your transaction
  • log the error
  • alert any user of the application
  • retry the operation
  • rethrow the exception

You may also wish to check if the exception is of a type you can handle before one of the above.

There is a good article on VB.NET exception handling here:

I see lots of other people have had a go at answering this in the mean time, I would recommend asking another question, and describing exactly what you are trying to achieve...

Colin Pickard
I suggest at least the top three for most SQL errors.
HLGEM
A: 

Now throw it ;P

LnDCobra
-1: bad idea. Screws up the stack trace.
John Saunders
+21  A: 

Rule of thumb for exception handling--If you don't know what to do with it, don't catch it.

Will
I disagree with this, and have edited my answer to reflect why.
SLC
+1 - and.. If you can't do anything useful about it here, don't catch it here.
Sky Sanders
@SLC - i think you are mis-interpreting the answer. Of course your application should not blithely crash due to an unhandled exception and an app level handler is usually a good idea. Catching an exception just because you can anticipate one not always the best design.
Sky Sanders
@Sky - There is a chance your application may crash, if the user performs a certain action, or if there are a certain set of circumstances such as a full hard disk or a lack of network connectivity. You know exactly what will make it crash. You don't choose to catch the exception, even though you can anticipate it. Can you explain _why_ you would not catch it?
SLC
@SLC - will and i have already explained exactly why. If you cannot do something useful with an exception where it is generated then there is no reason to catch it THERE. It is a design concern. Libraries are a prime example. Of course if you have exceptions bubbling all the way to the top with no AppDomain.UnhandledException handler then you have another issue entirely. Be clear on this, SLC, i am not suggesting to ignore exceptions.
Sky Sanders
@SLC Looks like you need to find out about how to handle exceptions and prevent your app crashing with **one** single general exception handler. Check out the documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.application.setunhandledexceptionmode.aspx
MarkJ
Then I think we're both confused about what each other is saying. Read your answer again. If you don't know what to do with an exception, don't catch it. So your database might throw an update exception, for one of a million reasons - you're saying if you don't know what to do with that exception, you shouldn't catch it. Nothing could be more wrong than that. Imagine if it throws an sql exception because your anti-viruses is blocking it, something you never predicted. You should catch the exception and display it, letting them continue, NOT ignoring it and letting your program crash and burn.
SLC
If you want to catch the exception somewhere else, or let it bubble up, _assuming your application can do this_ (which most can't, if you think about it, generally the code you write is not a library/component, there is nothing to bubble TO), then you are **catching the exception** which is exactly the opposite of your answer, so you are contradicting yourself.
SLC
Hard disk or network latency, if you are catching an exception or not it is too late anyway. Let the damn thing bubble up the call stack and handle the odd system fault at the top level of the app.
James Westgate
A: 

One thing to keep in mind is that in sections where you feel you do want to use a try/catch, you should move your Dim statements out of the try block. You can assign them values inside the block, but if you define them inside the try block, you won't have access to those variables inside the catch section as they will be out of scope at that point.

My standard practice in catch blocks, including trying to remediate the error if possible, is to write out to whatever logging mechanism I am using information about key variables that were involved in the section causing the error. It's not necessary, but I've found it often helps with debugging problems.

BBlake
A: 

FYI You dont have to use the catch to have a finally statement.

Sometimes people throw a new exception that is more firendly and add the original exception as the inner exception.

Often this is used to log the error to a file or send an email to an operator.

Sometimes, especially with Sql, it may just mean a duplicate key which means eg please choose another username, that one is already taken - its all down to your application.

James Westgate
+1  A: 

Hi Jonesy,

I'd like to suggest to you that if you do not know what to do with an exception, do not catch it. Too often, coders catch exceptions and just swallow them whole.

catch (Exception ex)
{
  /* I don't know what to do.. *gulp!* */
}

Clearly this isn't good, because bad things are happening, and no action is being taken. Only catch exceptions that are actionable!

That said, graceful error handling is important. Don't want your app to crash, right? You may find ELMAH useful for web applications, and a global exception handler is pretty easy to set up for WinForms or XAML desktop apps.

Putting all that together, you might find this strategy useful: 1. catch specific exceptions that you know might occur (DivideByZeroException, SQLException, etc.) and steer away from the generic catch-all Exception; 2. re-raise the exception after you handle it. e.g.

catch (SQLException ex)
{
  /* TODO: Log the error somewhere other than the database... */
  throw; // Rethrow while preserving the stack trace.
}

What can you really do with a SQLException? Did the database connection disappear? Was it a bad query? You probably don't want to add all the handling logic for that, and besides, what if it's something you didn't expect? So just handle the exception if you can, re-raise it unless you're certain it's resolved, and gracefully handle it at a global level (e.g. Show a message like "Something went wrong, but you might be able to continue working. Detailed info: '[ex.Message]'. Abort or Retry?")

HTH!

John Saunders, thanks for the correction.

Todd Sprang
Careful logging and re-throwing. If you log at each level, you could end up with many, many entries of the same exception in your error log. Google "log rethrow" for a few tips on the right way to go.
WCWedin
@JohnSaunders You should consider deleting your comment now that the answer has been fixed.
Mark Hurd
@Mark: thanks. I didn't know about the change. Downvote removed.
John Saunders
+5  A: 
Joel Coehoorn