VB.NET Try / Catch在运行时没有捕获异常

时间:2014-01-29 20:47:15

标签: vb.net try-catch

我正在编译一个控制台应用程序,由于某种原因停止捕获异常。作为测试,我提出以下内容:

Sub Main()
    Try
        Log = New LogWriter("C:\app.log")
        Log.Log("Test:")
        Dim a As Single = 5 / 0
        Dim b As Object
        Log.Log(b.ToString)
    Catch exTest As Exception
        Log.Log("Caught exception")
    End Try
End Sub

(其中LogWriter是我编写的用于控制日志输出的自定义类)那么,当我在目标机器上运行它作为编译的EXE(没有Visual Studio)时会发生什么?我在日志中得到“Test:”行,然后:

未处理的异常:System.NullReferenceException:未将对象引用设置为对象的实例。

为什么呢?我怎么做才能禁用异常处理?

2 个答案:

答案 0 :(得分:1)

结果无穷大是浮点数的有效值。

    Private Sub Test_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    Try
        Log("C:\app.log")
        Dim a As Single
        a = 5 / 0
        Log("Test zero: " & a)
        Dim b As Object
        Log(b.ToString)
        Log("Test null")
    Catch exTest As Exception
        Log("Caught exception" & vbCrLf & exTest.Message)
    End Try
End Sub
Sub Log(msg As String)
    Debug.Print(msg)
End Sub

结果:

C:\app.log
Test zero: Infinity
Caught exception
Object reference not set to an instance of an object.

b未实例化,因此错误。

答案 1 :(得分:0)

就我而言,我的应用程序需要操作AutoCAD绘图。添加对命名空间的引用Autodesk.AutoCAD.Runtime由于某种原因中断了try / catch机制。我删除了该命名空间并将引用留给了其他命名空间,并且它可以工作。