One of my method (Method1) spawns a new thread.
That thread execute a method (Method2) and during exectution an exception is thrown.
I need to get that exception information on the calling method (Method1)
Is there someway I can catch this exception in Method1 that is thrown in Method2?
In .NET 4 and above, you can use
Task<T>class instead of creating new thread. Then you can get exceptions using.Exceptionsproperty on your task object.There are 2 ways to do it:
In a separate method: // You process exception in some task’s thread
In the same method: // You process exception in the caller’s thread
Note that the exception which you get is
AggregateException. All real exceptions are availible throughex.InnerExceptionsproperty.In .NET 3.5 you can use the following code:
// You process exception in the child’s thread
Or // You process exception in the caller’s thread