Err.Number vs try-catch in VB.net

I inherited an old VB.net project. Mostly the code uses try-catch to handle errors. However, in some places I found If Err.Number <> 0 Then .

If an error occurs, what decides whether to throw an Exception or just set Err ?

I do not want to handle errors in both directions ...

+4
source share
2 answers

The Err object is used with the old On Error error handling construct, which is the remainder of the classic VB. Try-Catch is a more modern style of .NET error handling.

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

+3
source

It looks like the old code used On Error Resume Next. Make sure you understand what he is doing, kinda weird!

docs explain it

In the "Error at restart" window that appears, execution will continue to execute the instruction immediately after the statement that caused the runtime error, or with the statement immediately after the most recent call to the procedure containing the "Force resume next" statement. This statement allows you to continue execution, despite a runtime error. You can place the error handling procedure where the error will occur, and not transfer control to another place inside the procedure.

Then you should use If Err.Number <> 0 to check if an error has occurred.

+1
source

Source: https://habr.com/ru/post/1341008/


All Articles