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

c#, exception, multithreading

Solution

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).

Problem

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

Original source