Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to fail triggering the AppDomain's unhandled exception handler from a Task continuation?
    primarykey
    data
    text
    <p>I have a <code>Task</code> which runs asynchronously and handles exceptions using a task continuation <code>task.ContinueWith(t =&gt; ..., CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, taskScheduler)</code>.</p> <p>This works great for handling exceptions I know how to handle (e.g. <code>WebException</code>), but what if something like <code>NullReferenceException</code> is thrown, which I can't handle correctly?</p> <p>I want to be able to handle it using the AppDomain's unhandled exception handler (which in our case logs the error, pops up a dialog notifying the user, and exits the application), but so far I have not found a good way to do this.</p> <p>Simply rethrowing the exception from the continuation results in the unhandled exception handler being called when the task is finalized (because the rethrown exception is unobserved). This is not good because it may be seconds or even minutes after I know the application should crash.</p> <p>Calling <code>Environment.FailFast()</code> does crash the application but does not call the unhandled exception handler.</p> <p>Calling <code>Thread.CurrentThread.Abort()</code> calls the unhandled exception handler but only displays information/the stack trace from the <code>ThreadAbortException</code>. Plus it just seems like a bad idea to use this method.</p> <p>Calling <code>Application.OnThreadException()</code> actually does exactly what I want, except that it requires referencing <code>System.Windows.Forms</code> and handling <code>Application.ThreadException</code> which will not work with, for example, a service with no UI.</p> <p>Calling <code>task.Wait()</code> does not make sense because this in our case there's no place to call it from. If the task succeeds the result is processed using a success continuation, if it fails the exception continuation is called, and we can't block the thread creating the task (that's the whole point of the task). </p> <p>I could call the unhandled exception handler directly except that in at least one case that would require adding a dependency I don't want to add in my application.</p> <p>Am I missing an obvious way to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload