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

  • Thread starter Vijayakrishna Pondala
  • 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());
}
}
 
J

Juan Gabriel Del Cid

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
 
J

José Joye

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é
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top