Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Err.Number vs try-catch in VB.net

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...

like image 411
leiflundgren Avatar asked Feb 23 '11 17:02

leiflundgren


People also ask

What is a try catch in Visual Basic?

In visual basic, Try Catch statement is useful to handle unexpected or runtime exceptions which will occur during execution of the program. The Try-Catch statement will contain a Try block followed by one or more Catch blocks to handle different exceptions.

What is error handling in VB?

The Visual Basic error handling model allows programmers to perform special actions when an error occurs, such as jumping to a particular line of code. When an exception occurs in the Active Expert, the standard Visual Basic error handling works as expected.

What is a catch error?

The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error.


1 Answers

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.

like image 113
Fredrik Mörk Avatar answered Sep 23 '22 18:09

Fredrik Mörk