Err.Number vs try-catch in VB.net

10,370

Solution 1

The Err object is use with the old-style On Error error handling construct, that is a remainder from classic VB. Try-Catch is the more current .NET style of error handling.

You can learn more about this, and the difference in Error Handling in Visual Basic.NET.

Solution 2

Sounds like the old code was using On Error Resume Next. Make sure you understand what it does, its kind of odd!

The docs explain it

On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error, or with the statement immediately following the most recent call out of the procedure containing the On Error Resume Next statement. This statement allows execution to continue despite a run-time error. You can place the error-handling routine where the error would occur rather than transferring control to another location within the procedure.

You would then use If Err.Number <> 0 to check whether an error had occurred.

Share:
10,370
leiflundgren
Author by

leiflundgren

Works daily to develop VOIP solutions. Mostly in C#/C++, but whatever does the job is used.

Updated on June 25, 2022

Comments

  • leiflundgren
    leiflundgren almost 2 years

    I have inherited an old VB.net-project. The code mostly uses try-catch for error-handling. However in some places I have found If Err.Number <> 0 Then.

    If an error occurs, what decides if an Exception should be thrown, or just setting Err?

    I don't want to handle error both ways...