473,396 Members | 1,998 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,396 software developers and data experts.

Exceptions vs. Events

I have a class written in C# which uses a worker thread
internally to do some processing. It's possible that an
exceptional situation can arise in the worker thread, and as
the class is presently written, it throws an exception in
those cases.

But I don't think this is a good design. I don't see how
these exceptions can be caught. Plus, exiting a thread by
throwing an exception probably isn't very clean either.

So I was thinking about using events instead. When an
exceptional situation arises in the worker thread, have it
raise an event letting clients know about the problem.

The XmlValidatingReader class use an ValidationEventHandler
for passing exceptions through events, so there is a
precedence for this approach.

Anyway, I'd appreciate advice. Thanks.
Nov 16 '05 #1
2 1428
Wavemaker <ja**********@BiteMeHotmail.com> wrote:
I have a class written in C# which uses a worker thread
internally to do some processing. It's possible that an
exceptional situation can arise in the worker thread, and as
the class is presently written, it throws an exception in
those cases.

But I don't think this is a good design. I don't see how
these exceptions can be caught. Plus, exiting a thread by
throwing an exception probably isn't very clean either.

So I was thinking about using events instead. When an
exceptional situation arises in the worker thread, have it
raise an event letting clients know about the problem.

The XmlValidatingReader class use an ValidationEventHandler
for passing exceptions through events, so there is a
precedence for this approach.

Anyway, I'd appreciate advice. Thanks.


If you want to use a custom threadpool for this, mine happens to do
exactly what you want :)

See http://www.pobox.com/~skeet/csharp/miscutil and look at the
CustomThreadPool class.

Of course, there's always AppDomain.UnhandledException, too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Wavemaker" <ja**********@BiteMeHotmail.com> wrote in message
news:lO********************@comcast.com...
I have a class written in C# which uses a worker thread
internally to do some processing. It's possible that an
exceptional situation can arise in the worker thread, and as
the class is presently written, it throws an exception in
those cases.

But I don't think this is a good design. I don't see how
these exceptions can be caught. Plus, exiting a thread by
throwing an exception probably isn't very clean either.
This approach relies on implementation defined behavrior.

From the C# Language Specification, ECMA-334 2nd edition - December 2002,
section 23.3 which talks about how exceptions are handled.

"If the search for matching catch clauses reaches the code that initially
started the thread, then execution
of the thread is terminated. The impact of such termination is
implementation-defined."
So I was thinking about using events instead. When an
exceptional situation arises in the worker thread, have it
raise an event letting clients know about the problem.
Care must be taken in this approach to prevent deadlock if the controlling
thread ever blocks waiting for the worker thread.
The XmlValidatingReader class use an ValidationEventHandler
for passing exceptions through events, so there is a
precedence for this approach.

Anyway, I'd appreciate advice. Thanks.


I use the same approach, raise an event in the worker thread when an
exception is thrown. I do something like the following

void ThreadMethod()
{
try
{
ActuallyDoThreadStuff();
}
catch (ThreadAbortException e)
{
// Ignore exception since shutting down isn't considered an error.
}
catch (Exception e)
{
RaiseExceptionEvent(e);
}
}
Nov 16 '05 #3

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

Similar topics

11
by: C# Learner | last post by:
What type of exception should I throw when my code detects that a connection has dropped (i.e. NetworkStream.Read() returns 0)? Should I just throw a SocketException or should I create my own...
9
by: Steven Blair | last post by:
Hi, I need to catch exceotions on File.Delete() After checking the help, I have noticed that thgere are serevral Exceptions that can be thrown. My question is, should I catch all thes...
5
by: Miyra | last post by:
Hi. I'm working with an app that uses exceptions for control flow. These are code blocks where exceptions are thrown/caught regularly. A couple hundred exceptions occur per hour and they're caught...
10
by: Razzie | last post by:
Hi all, The title of this post may sound a bit weird, but I was wondering about the following nonetheless. I have a class libray containing, say, 4 classes: A, B, C, D. Class A somehow has a...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
2
by: Olie | last post by:
I have a little scinario and I was wondering if someone could point me in the best direction for coding it. I have a custom user control that plays a DVD and it starts another thread that...
42
by: Jon Harrop | last post by:
Why are exceptions in C++ ~6x slower than exceptions in OCaml? -- Dr Jon D Harrop, Flying Frog Consultancy Objective CAML for Scientists...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
24
by: usenet | last post by:
I am unable to catch floating exceptions (e.g. divide by 0 or 0/0) using the standard exceptions defined in stdexcept. What is the recommended way to catch such exceptions? Thanks, Song
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.