Exception Handling and Class Inheritence

O

Oblivion

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);
}
}
}
 
M

My interest

The timer probably is running under a separate thread, i.e. is
independent from UseBase()
 
B

Bruce Wood

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.
 

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