是否有可能在VB .Net控制台应用程序中捕获启动/初始化异常(即在Main(...)运行之前)?
有许多解决方案在Main中为Thread和Unhandled异常添加处理程序,但是在Main运行时,捕获启动/初始化异常为时已晚。
例如,是否可以捕获此异常:
Module Module1
Private Class BrokenClass 'Constructor is broken/misconfigured; throws exception
Public Sub New()
Throw New Exception("I Crashed!") 'Emulate constructor crash
End Sub
End Class
'---------------------------------------------------------------------------------------------------
Dim obj As BrokenClass = New BrokenClass() 'Assignment causes constructor exception
'---------------------------------------------------------------------------------------------------
Sub Main(ByVal args As String()) 'Main hasn't run yet. No chance to assign handlers.
Console.WriteLine("Main Running...Press ENTER to exit")
Console.ReadLine()
End Sub
End Module
(我为色彩错误道歉;显然VB在代码块中没有正确着色)