I\'ve started using try-catch blocks (a bit late, I know!), but now I\'m not sure what to do with the exception once I\'ve caught it. What should I do?
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.
What do you want to do? It depends entirely on your application.
You could have it retry, you could display a message box 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 a 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 message box saying 'Error' because it's infinitely better than "This program has performed an illegal operation and will be closed."
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 an 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.
I see a lot of recommendations not to catch it if you don't know what to do with it. That's only sort of right. You should be catching exceptions, but maybe not at this level. Using your code for an example, I'd write it more like this:
Using connection As New SqlConnection("connection string here"), _
sqlCmd As New SqlCommand("do some SQL", connection), _
sqlDa As New SqlDataAdapter(sqlCmd)
sqlDa.Fill(dt)
End Using
Not only is there no Try/Catch/Finally, there's no open or close. The .Fill() function is documented as opening the connection if required, and the Using block will make absolutely certain it's closed correctly, even if an exception is thrown.
This leaves you free to catch exceptions at a higher level — in the UI or business code then calls the function where your query runs, rather than right here at the query itself. This higher-level code is in a much better position to make decisions about how to proceed, what logging needs to happen or show a better error message to the user.
In fact, it's this ability for exceptions to "bubble up" in your code that makes them so valuable. If you always catch an exception right there where it's thrown, then exceptions aren't adding much value beyond the vb's old "On Error Goto X" syntax.
It depends on your business logic, but you may want to do one or more of the following.
You may also wish to check if the exception is of a type you can handle before one of the above.
A good article on VB.NET exception handling is Exception Handling in VB.NET by Rajesh VS.
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".