.net - 为什么 try/catch 没有捕获访问冲突?

标签 .net vb.net exception exception-handling

我在一些偶尔抛出预期异常的代码周围放置了一个 try/catch 块,但它没有捕获它并显示消息框,而是停止调试器并提醒我异常未处理。

如何处理此异常,以便我的代码在异常发生时不会停止?

enter image description here

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/

相关文章:

c# - WCF 中的FaultException 与异常

c# - 异步方法不调用

java - 为什么会使用原因链中的循环?

vb.net - 从父窗体 MenuStrip 中删除 ToolStripMenuItem

python - 将异常主体存储在变量中

java - 查询 Hibernate 实体时出错

.net - 在 MS Project 2003 中嵌入我的 .NET 控件

.net - WPF 列表框项目不自动换行

c# - Hangfire 和 VB.NET - 获取应用程序启动类中配置的内容

c# - “字段”不是 'System.Data.DataRow' 的成员