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 <>开发者_StackOverflow; 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...
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.
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.
精彩评论