473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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());
}
}
Nov 15 '05 #1
2 1574
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
Nov 15 '05 #2
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é

"Juan Gabriel Del Cid" <jd*****@atrevido.nospam.net> a écrit dans le message
de news:eh**************@TK2MSFTNGP12.phx.gbl...
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

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: SEB | last post by:
How to catch errors thrown by the base class constructor in c# app? Any help greatly appreciated.
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
4
by: chris | last post by:
Hi, I write some code guarded with exception handling... simplified code look like this... int main(int argc, char* argv){ try{
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
2
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.