开发者

What error handling should be used in VB.NET

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开发者_StackOverflow中文版 { ... }" pattern?


"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


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?


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


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.


Old post, but here's my two penneth. Debugging with On Error Goto is a lot easier than Try Catch:

On Error Goto ErrHand
do something that goes wrong
do something else

ErrHand:
Resume Next

Place a breakpoint on Resume Next and if it hits, step through your code once and you jump to the statement directly after the one that caused the exception, making it a lot easier to track down the problem.

On Error Goto is also good if you expect errors, like resources that are created by other processes that you are not in control of and aren't yet available, or locked resources when you know other processes will lock a resource and you must wait your turn. With On Error Goto you can wait for a bit and then Resume Retry, where Retry is a label in your code to try the operation again. When you look at code with that structure it's very obvious what's going on:

  On Error Goto ErrHand

  dim ErrCnt as Integer = 0
  do something

RetryFile:
  On Error Goto FileErr
  download a file

  On error Goto ErrHand
  do something with the file

  On Error Goto 0
  Goto Done

FileErr:
  ErrCnt += 1

  if ErrCnt < 10 then
    sleep for a while
    Resume RetryFile
  else
    Throw New Exception("Can't download the file")
  end if

ErrHand:
  Throw New Exception(Err.Number & ": " & Err.Description)

Done:
  tidy up

One last point. I use both structures as I do like the Try Catch nesting, but as can be seen from the above, similar functionality can by achieved with On Error Goto, and in some ways it's neater as it moves the error handling out of the flow of the main code and all into one place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜