【发布时间】:2012-02-14 15:05:30
【问题描述】:
我正在开发一个我继承的应用程序,它内置了很多过于广泛的异常,它使用 try/catch 块但完全忽略了异常(在大多数情况下)并跳过。我想包装一个更好的自定义异常模型,但我的自定义异常本身一直未处理。我将如何抛出可以记录的异常并将异常标记为已处理以继续执行?
在示例中,我进入 finally 块,并进入 ExpectedExeptionType。然而,它仍然抛出一个未处理的异常,通过弹出框停止线程执行。有没有办法让我抛出异常,用它来记录消息,然后继续处理(将我的异常算作处理它)?
例子:
Module Module1
Sub Main()
Dim a As Integer
Console.WriteLine("Please enter an integer and then press enter.")
Try
a = Console.ReadLine()
Console.WriteLine("You entered the value " & a.ToString)
Console.WriteLine("Press enter to continue")
Console.ReadLine()
Catch ex As Exception
Throw New ExpectedExceptionType("Bad")
Finally
'Clean up objects/whatever and continue
Console.WriteLine("TEST")
Console.ReadLine()
End Try
Console.WriteLine("Hi there, press enter to exit!")
Console.ReadLine()
End Sub
Public Class ExpectedExceptionType
Inherits Exception
Public Sub New()
End Sub
Public Sub New(ByVal Message As String)
MyBase.New(Message)
Console.WriteLine("I know I'm getting into the exception.")
End Sub
Public Sub New(ByVal Message As String, ByVal Inner As Exception)
MyBase.New(Message)
End Sub
End Class
结束模块
【问题讨论】:
-
"我没有进入 finally 块" --> finally 块总是运行,不管有没有异常。
-
对不起,你就在那儿。它确实做到了最后 - 这是我试图弄清楚的未处理的异常触发。我将重新表述这个问题......但我真的很好奇为什么我会从我的自定义异常中生成一个未处理的异常。
标签: vb.net syntax exception-handling