我在一些偶尔抛出预期异常的代码周围放置了一个 try/catch 块,但它没有捕获它并显示消息框,而是停止调试器并提醒我异常未处理。
如何处理此异常,以便我的代码在异常发生时不会停止?
Friend myDevInfo As New devInfo
''' <summary>
''' Closes the device handle obtained with CreateFile and frees resources.
''' </summary>
'''
Friend Sub CloseDeviceHandle()
Try
WinUsb_Free(myDevInfo.winUsbHandle)
If Not (myDevInfo.deviceHandle Is Nothing) Then
If Not (myDevInfo.deviceHandle.IsInvalid) Then
myDevInfo.deviceHandle.Close()
End If
End If
Catch ex As System.AccessViolationException
MsgBox("System.AccessViolationException")
Catch ex As Exception
Throw
End Try
End Sub
最佳答案
try...catch
当没有附加调试器时,块应该按预期工作。
您可以在 Debug -> Exceptions 下定义调试器中断的异常,我相信默认值是中断 AccessViolationException
.
关于.net - 为什么 try/catch 没有捕获访问冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312296/