Why am I unable to catch exceptions thrown in static constructor?

  • Thread starter Thread starter Vijayakrishna Pondala
  • Start date Start date
V

Vijayakrishna Pondala

Hi,
I am not able to catch exception that has been thrown from a static
constructor. I am using .NET v1.0. I am getting 'Unhandled Exception....'
Help!!

Here is my code snippet:

public class StaticTest
{
public StaticTest()
{
}
static StaticTest()
{
int i = 0;
try {
int j = 12/i;
} catch (Exception e) {
throw e;
}
}
static bool IsTrue()
{
return true;
}
}
I am calling this method from another class like this:
class A
{
//
public bool IsTrue()
{
try {
return StaticTest.IsTrue();
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
 
Classes get loaded by the runtime "somewhere else." By this I mean that your
code does not load the class StaticTest, therefore, it is unable to catch
the exception it throws whilst initializing. If you want to catch it, you
have to load the class by yourself using reflection.

Hope that helps,
-JG
 
I do also have the same problem :-(

Could you elaborate a bit?
Say that all static members are assigned within the static Class constructor
(and this "randomly" throw an exception). What do you do with reflection and
at what time do you play with it?

José
 
Back
Top