By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,161 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,161 IT Pros & Developers. It's quick & easy.

Exception Handling and Class Inheritence

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
The timer probably is running under a separate thread, i.e. is
independent from UseBase()

Feb 1 '07 #2

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.