Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you rethrow a .NET exception on a different thread?

Is it legal and safe in C# to catch an exception on one thread, and then re-throw it on another.

E.g. is this legal

Exception localEx = null;

Thread mythread = new Thread() { () =>
                   {
                        try
                        {
                            DoSomeStuff();
                        }
                        catch(Exception ex)
                        {
                            localEx = ex;
                        }
                    });

myThread.Start();
...
myThread.Join();

if(localEx != null)
   throw localEx;    // rethrow on the main thread

I think it is legal, but I'm having trouble finding any doco that proves it. The closest I found was a brief mention of transferring exceptions between threads here: http://msdn.microsoft.com/en-us/library/ms229005.aspx

like image 687
John Rusk Avatar asked Aug 20 '09 01:08

John Rusk


People also ask

When should you Rethrow an exception?

If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.

How can we Rethrow an exception in C#?

An exception caught by one catch can be rethrown so that it can be caught by an outer catch. To rethrow an exception, you simply specify throw, without specifying an expression.

What type of statement is used to Rethrow an exception?

A Throw statement with no expression can only be used in a Catch statement, in which case the statement rethrows the exception currently being handled by the Catch statement. The Throw statement resets the call stack for the expression exception.

What is Rethrow C#?

In C# 5.0, a mechanism was added that enables the throwing of a previously thrown exception without losing the stack trace information in the original exception. This lets you rethrow exceptions, for example, even from outside a catch block and, therefore, without using an empty throw.


5 Answers

Yes, it's legal. Exceptions are (generally speaking) descriptive objects with no thread affinity.

You'd be better off wrapping your thread exception in a new exception:

throw new Exception("Something descriptive here", localEx);

That way, the stack trace in localEx will be preserved (as the InnerException of the new exception).

like image 194
Ben M Avatar answered Oct 18 '22 13:10

Ben M


What you're doing is not a rethrow. It's a new throw of an exception instance you happened to have in a variable. Even if you were using only a single thread, this would be a bad idea, as it makes the exception look like it came from the "throw" site. With multiple threads, I have no idea how anyone would figure out there had been a thread change.

like image 27
John Saunders Avatar answered Oct 18 '22 14:10

John Saunders


Absolutely. System.AggregateException is added to .NET 4 for specifically that purpose during parallel operations.

like image 26
Sam Harwell Avatar answered Oct 18 '22 13:10

Sam Harwell


I don't see why it wouldn't work, but you need to remember that you aren't actually rethrowing the exception. You are throwing a new exception, that just happens to be the same exception object. So, for example, the stack trace will say it was thrown from "throw localEx;" instead of wherever the original exception came from.

like image 27
Tal Pressman Avatar answered Oct 18 '22 14:10

Tal Pressman


It is legal and it isn't a rethrow, it's a new exception being thrown on another thread (with the same exception object)

like image 20
Daniel Rodriguez Avatar answered Oct 18 '22 14:10

Daniel Rodriguez