I want to catch an exception in a thread A, then pass the exception object to a thread B, and throw it from thread B. Is that safe?
Thread A
try {
// Code that throws exceptions
} catch (Exception e) {
sendToOtherThread(e);
}
Thread B
Exception e = receiveException();
throw e;
EDIT
For sake of clarity: I understand how threading works, and how I should pass the object reference from one thread to the other. The question is more about is it safe to throw an exception object from one thread that did not create it or is there any problem with the class Exception itself or the way .NET handles it.
You can rethrow an exception received from another thread (for example, in a BackgroundWorker’s RunWorkerCompleted event handler), but it would be more usual to wrap it in another exception, to preserve the stack trace: