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...
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With