Is there a way to catch these Exceptions? (I am not looking for “just put try/catch or on error goto around them”). I am trying to find out if these errors can be caught by something outside of them.
Public Class ABC
Shared Sub New()
throw new Exception("Ha!")
End Sub
End Class
or this:
Public Class ABC
Dim obj as Object = CreateObject()
Public Function CreateObject()
throw new Exception("Ha!")
End Function
End Class
BTW, Putting these in my startup class’s (it’s a Windows form) as the first lines in “Shared Sub New()” isn’t working:
AddHandler Application.ThreadException, AddressOf Application_ThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
See this:
http://temujin.blogsome.com/2006/03/15/static-constructors-and-exceptions/ (now dead – archive)
Key points:
So try looking for a TypeInitializationException at the application/AppDomain level. Also:
In a nutshell – don’t do anything that might throw exceptions in static (Shared) constructors. Move the code to static properties or methods or an instance constructor.