How to Bubble Up Exception Information? : Easily Explained with Examples
You might have come across situations where we have to offload execution of some tasks to some other thread so that it can be done in the background. When this child thread is executed there might be situations where it throws exception and often these exceptions is unknown to the caller thread or the main thread. In those cases we need to bubble up the exception which is occurring in the child thread to the parent where it can be handled.
What is meant by Bubble Up Exception Information?
There are different ways by which we can bubble up the exception information from the child thread to the main thread. In .Net 4.0 and above we can use Task through which it is much easier to achieve this as shown below:
As shown in the figure above simple Try-Catch is needed to catch the exception thrown by the Task. Exception thrown is shown below:
But instead of Task if we use Thread to execute some task in the background it has to be handled differently as shown in the figure below:
As shown in the figure above we have to send the exception object to the child thread and monitor the exception object for any exception thrown once the child thread finishes its execution.
Exception handled is as shown in the figure below:
This comment has been removed by the author.
ReplyDelete