473,513 Members | 3,208 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception Handling and Class Inheritence

Hi All,

I have an issue where I get an "Unhandled Exception" pop up box when
an error occurs in an inherited base class even tho there's a
try..catch written in the inheriting class to handle it. I've got an
example of the code below to give you an idea of what's occuring. I
thought the try..catch took care of everything... do I HAVE to write
the try..catch logic in the base class? Is this a quirk with class
inheritence or .NET?

Thanks in advance for all replies!
************************************************** ************************************************** **
Code:

public class BaseClass
{
private int myProperty=0;
public int MyProperty
{
get {return myProperty;}
set {myProperty = value; if (myProperty 1) {timer1.start()}
else {timer1.stop()}
}
private timer1_tick(object sender, eventargs e)
{
DoSomething(); // EXCEPTION OCCURS HERE!!
}
}

public class InheritedClass : BaseClass
{
public InheritedClass()
{
// CONSTRUCTOR...
}
public void UseBase()
{
try
{
this.MyProperty = 5; // This should kick off the
timer....
}
catch (Exception e)
{
WriteError(e.Message);
}
}
}

Feb 1 '07 #1
2 1193
The timer probably is running under a separate thread, i.e. is
independent from UseBase()

Feb 1 '07 #2
On Feb 1, 9:08 am, "Oblivion" <VijayKrishn...@gmail.comwrote:
Hi All,

I have an issue where I get an "Unhandled Exception" pop up box when
an error occurs in an inherited base class even tho there's a
try..catch written in the inheriting class to handle it. I've got an
example of the code below to give you an idea of what's occuring. I
thought the try..catch took care of everything... do I HAVE to write
the try..catch logic in the base class? Is this a quirk with class
inheritence or .NET?

Thanks in advance for all replies!
************************************************** ************************************************** ***
Code:

public class BaseClass
{
private int myProperty=0;
public int MyProperty
{
get {return myProperty;}
set {myProperty = value; if (myProperty 1) {timer1.start()}
else {timer1.stop()}
}
private timer1_tick(object sender, eventargs e)
{
DoSomething(); // EXCEPTION OCCURS HERE!!
}

}

public class InheritedClass : BaseClass
{
public InheritedClass()
{
// CONSTRUCTOR...
}
public void UseBase()
{
try
{
this.MyProperty = 5; // This should kick off the
timer....
}
catch (Exception e)
{
WriteError(e.Message);
}
}

}
Think carefully about the control flow here. From where is time1_tick
being invoked?
>From your code? From where, then? When you say timer1.start(), the
start() method of the timer doesn't directly call timer1_tick, does
it? It starts a timer, which _then_, some time _later_, calls
timer1_tick. So who is calling timer1_tick? It can't be your code,
because your code has moved on to do other things by then.

The only possible answer is that timer1_tick is being called on
another thread, independent from your code. If you look at the text of
the exception (you should have pasted it in, by the way) you'll see
that your timer1_tick method is being called from... well, for all
intents and purposes, from the O/S.

So, there are only two ways to deal with exceptions in timer1_tick.

1. Put a try...catch within timer1_tick and, if an exception is
caught, deal with it, report it, or package it up and marshal it back
onto your main thread using Invoke().

2. Use a global exception handler to catch the exception just before
your application dies (using AppDomain.UnhandledException and
Thread.ThreadException) and report it. Your application still dies,
though.

By the way, the same goes for exceptions inside Windows forms: the
Form code isn't called from where you think it is, so try...catch in
the caller doesn't work.

Feb 1 '07 #3

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

Similar topics

11
2837
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
7
5969
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
17
2452
by: Dave | last post by:
Hi I'm making a 3D Engine which consists of the class C3DEngine. Part of this engine is a file loader, a class called CMeshLoader. I have made an instance of CMeshLoader in C3DEngine, ie...
3
2736
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
3
2233
by: Tony Johansson | last post by:
Hello! When you allocate object dynamically which mean on the heap I find that a problem when using exception. What is the prefer method to handle this kind of problem. //Tony
2
2582
by: tom | last post by:
Hi, I am developing a WinForm application and I am looking for a guide on where to place Exception Handling. My application is designed into three tiers UI, Business Objects, and Data Access...
44
4162
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
1483
by: Peter Aitken | last post by:
It seems that some classes have their own exception handling built in. For example if I try to use FileStream to open a file that does not exist, my Try...Catch block seems to be ignored and...
1
3012
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.