LAI TOCA
2 min readJun 17, 2019

Exception handling in .Net Task

Ref: https://www.digit.in/how-to/apps/how-to-use-parallel-programming-with-c-24059.html

As we known exception handling was cornerstone for creating robust application(s). Unhandled exceptions might cause application crash or even went into an unpredictable way. As a programmer, we should try to manage any kind exceptions and control the application flow in more reliable approaches.

The below sample code snippet was traditional mistake we might make:

The global domain exception handler could not handle the exception inside from the task. The application crash and stop on the line-12.
Application Crash

We could add Try-Catch block for exception handling inside the task, so the application could execute in proper way.

We have noticed that we could register global unobserved task exception handler looks below:

Note that: the handler will raise while garbage collect and after task invoke its finalizer.

For the .Net Core only in Release mode will fire the handler.

You might curious if we want to handle the exception(s) immediately ? Maybe we should use method: ContiuneWith.

Or much simplest way:

Now we got diverse approach(es) for handling exception for the task:)

Reference

LAI TOCA
LAI TOCA

Written by LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)

No responses yet