Delegates and Exceptions

A

AC

Hi All,

I have been experimenting with delegates and exceptions.
I know that when an exception occurs on a separate thread
it is saved and retrieved when EndInvoke is called, but
why can't I rethrow it?

Regards,

Example Code:

class Application
{
delegate void MyDelegate();

static void Main(string[] args)
{
Application app = new Application();
app.Start();

Console.ReadLine();
}


public void Start()
{
MyDelegate myDelegate = new MyDelegate(BadMethod);

myDelegate.BeginInvoke(new AsyncCallback
(DelegateCallback), myDelegate);
}


public void BadMethod()
{
string newstring = string.Empty;

if (newstring.Length == 0)
{
throw new ApplicationException("delegate
testing exception");
}
}


private void DelegateCallback(IAsyncResult ar)
{
MyDelegate result = (MyDelegate) ar.AsyncState;

try
{
result.EndInvoke(ar);
}
catch
{
throw;
}
}
}
 
J

Jon Skeet [C# MVP]

AC said:
I have been experimenting with delegates and exceptions.
I know that when an exception occurs on a separate thread
it is saved and retrieved when EndInvoke is called, but
why can't I rethrow it?

You can - what makes you think you can't?

Note that in your example, when your exception is rethrown, nothing is
reporting it because it's in a threadpool thread without any extra
exception handlers.
 
A

AC

Hi Jon,


Thanks for the reply. How would I catch exceptions
from the thread pool? I've tried registering a handler
for the AppDomain.UnhandledException to no avail.

Thanks
 
D

David Levine

AC said:
Hi Jon,


Thanks for the reply. How would I catch exceptions
from the thread pool? I've tried registering a handler
for the AppDomain.UnhandledException to no avail.
The threadpool itself catches and swallows all exceptions thrown on its
threads, so you will not get an unhandled exception. You need to catch all
the exceptions thrown in your threadpool callback routine and then
propagate that exception using your own mechanism. You can queue it up,
report it immediately, etc.
 
A

AC

Sorry I guess I'm being thick here. But that was what I
wanted to do in the first place. In the callback routine
I simple wanted to throw the exception so it gets
propagated up the call stack.

Would it be possible to point me at a simple example?

Thanks again.
 
J

Jon Skeet [C# MVP]

AC said:
Sorry I guess I'm being thick here. But that was what I
wanted to do in the first place. In the callback routine
I simple wanted to throw the exception so it gets
propagated up the call stack.

Would it be possible to point me at a simple example?

Which thread's stack are you expecting it to get propagated up?
 
D

DT

I thought that because the callback executes on the main thread in my
example I could propagate the exception on that thread.

I guess my new question is how to detect exceptions in the threadpool?

Thanks again
 
J

Jon Skeet [C# MVP]

DT said:
I thought that because the callback executes on the main thread in my
example I could propagate the exception on that thread.

No - the callback *doesn't* execute on the main thread. It can't,
because the main thread is busy doing something else by then - that's
the point of it being an asynchronous call in the first place.
I guess my new question is how to detect exceptions in the threadpool?

Well, you've got your callback - you can put whatever code you want in
there, including a big try/catch round whatever else is normally there.
 
D

DT

But the callback isn't the asynch process? Isn't the whole concept of a
callback to notify the caller that the asynch process has completed?
 
J

Jon Skeet [C# MVP]

DT said:
But the callback isn't the asynch process? Isn't the whole concept of a
callback to notify the caller that the asynch process has completed?

No, it's to allow something to happen after the main async process has
completed. That "something" may involve the caller, but it may not.

Put it this way - after your call to BeginInvoke, the main thread goes
on its merry way and executes some more code. What would you expect the
stack trace to look like in the main thread when the callback was
invokved? Would you expect it to just interrupt the main thread
wherever it was? The nightmare of re-entrancy would be horrible in that
case.
 
D

David Levine

If you just want to capture the exception information you can do something
like this...wrap your callback with a try-catch and in the catch block
simply save the exception object into a queue; this executes in the context
of the threadpool thread. You can then set a flag or an event to notify the
main thread that new data is available and it can pull the exception object
out of the queue.

DT said:
I didn't realise that. Thanks for your help

Regards
 
D

DT

Thanks David

David Levine said:
If you just want to capture the exception information you can do something
like this...wrap your callback with a try-catch and in the catch block
simply save the exception object into a queue; this executes in the
context of the threadpool thread. You can then set a flag or an event to
notify the main thread that new data is available and it can pull the
exception object out of the queue.
 

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