views:

43

answers:

4

What error handling should be used in VB.NET?

Should one use the "On Error Goto ErrorHandler ... Exit Sub ErrHandler ... End Sub" pattern or should you use the "try { ... } catch { ... } finally { ... }" pattern?

+6  A: 

"try { ... } catch { ... } finally { ...}" pattern by a long shot.

C#

try
{
// Do Something that may go wrong
}
catch (Exception ex)
{
//Do something with the error
}
finally
{
//Cleanup
}

or

VB

Try

// Do Something that may go wrong

Catch ex  as Exception

//Do something with the error

Finally

//Cleanup

End Try
Barry
+1, Except that the curly braces aren't part of VB!
Martin
lol! This is very true - I was being extremely lazy there and copy and pasted from the OP. I have edited my answer to include both language examples.
Barry
@barry: why choose the try catch pattern?
Craig Johnston
@Craig There are so many reasons why you should use try catch. I personally find it more logical. Try doing this section of code, if it fails then catch the exception and carry on if you can. I find OnError extremely difficult to follow and very messy. There is nice article on MSDN about life without OnError http://msdn.microsoft.com/en-us/library/aa289194(VS.71).aspx
Barry
@Barry: can you refer to the Err object when using "try ... catch" or are you forced to use the Exception object?
Craig Johnston
@Craig Johnston: You should be using `Exceptions`, they are much more flexible that the old `Err`.
ho1
@Craig No you use the Exception object. Although `Exception` should be the very last exception to catch. You should always list exceptions in order. So specific execptions come first flowing down to the general `Execpetion` last. So if you are connecting to SQL you should catch `SQLException` first as this will contain detailed error information specific to SQL.
Barry
A: 

On Error Goto ErrorHandler ... Exit Sub ErrHandler ... End Sub is from the VB6 days. Definitely go with Try... Catch... Finally...

Shevek
A: 

A little background

'On Error Goto' is the way things were done in VB 6 before the .Net days. The VB compiler still allows this so you can easily port old VB code to VB.Net. VB.Net is probably the only .Net language that supports this.

'Try Catch Finally' is the .Net way to do things and a lot more flexible allowing you to catch, wrap and rethrow exceptions. It allows for easier interoperation between components written in different languages and is a lot more readable when you do more complex error handling because you don't have goto's.

Mendelt
A: 

The most obvious reasons I can think of off the top of my head to steer clear of On Error GoTo... would have to be:

  1. On Error GoTo does not discriminate between types of exceptions.
  2. On Error GoTo does not provide as much structure as Try/Catch/Finally (e.g., nesting one Try/Catch block within another).
  3. On Error GoTo has no counterpart to Finally (that I know of).

I'm sure in many cases, clever use of On Error GoTo could mimic the behavior that is built in to VB.NET's Try/Catch/Finally feature. But what would be the point?

Dan Tao