Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:19:55+00:00 2026-06-10T15:19:55+00:00

What is the recommended way to handle errors when using Tasks to unblock the

  • 0

What is the recommended way to handle errors when using Tasks to unblock the UI and using Aync/Await is not an option?

I typically want to handle expected errors by updating the UI and handle unexpected Errors (log Exception(s), inform user and shutdown app) in a global error handler like Application_DispatcherUnhandledException.

I am playing with the following approach which looks ugly to me.

Private Sub _showAsyncButton_Click(sender As Object, e As RoutedEventArgs) Handles _showAsyncButton.Click
Dim task = New WebClient().DownloadStringTaskAsync("http://www.microsoft.com")
'Dim task = New WebClient().DownloadStringTaskAsync("forcing error in async I/O")
task.ContinueWith(
    Sub()
        _resultField.Text = task.Result
        'Throw New ApplicationException("forcing error in End method")
    End Sub, Nothing, Nothing, TaskScheduler.FromCurrentSynchronizationContext
    ).ContinueWith(
        Sub(a)
            Select Case True
                Case TypeOf (a.Exception.GetBaseException) Is WebException AndAlso CType(a.Exception.GetBaseException, WebException).Status = WebExceptionStatus.NameResolutionFailure
                    'Handle expected error
                    Dispatcher.Invoke(Sub() MessageBox.Show("Cannot access web site. Please try again later"))
                Case Else
                    'Rethrow unexpected error in global error handler
                    Dispatcher.BeginInvoke(Sub() ExceptionDispatchInfo.Capture(a.Exception).Throw())
                    'ExceptionDispatchInfo.Capture(aex).Throw() 'Does not work 
            End Select
        End Sub, TaskContinuationOptions.OnlyOnFaulted)

End Sub

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:19:57+00:00Added an answer on June 10, 2026 at 3:19 pm

    Handling exceptions in a parallel application alongside handling many other stuff is a painful task and requires sufficient knowledge and skill. Consider re-writing the above code using async-await keywords introduced in .Net 4.5, it then allows you to handle exceptions in the same way you did in a synchronous programming model. Of course, there exists some situations when async-await is not adequate and you need to directly use TPL API.

    You could then use a .Net decompiler (i.e. .Net Reflector) to see how much work compiler did on behalf of you, it would also be an invaluable learning source.

    Also Consider taking a look at Exception Handling with the Task Parallel Library which provides you with a good insight on how to deal with exceptions when doing parallel programming using TPL.

    The “The zen of async: Best practices for best performance” presented by Stephen Toub also dives deeply into how asyncworks under the hoods in .Net 4.5 and it covers some advanced topics which exception handling is one of those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The recommended way to handle optimistic locking in a RESTful interface seems to be
Is there a recommended way of using Django to clean an input string without
What is the recommended way to handle Nick / Email taken AJAX validation in
What is the recommended way to get a handle to the global object in
what is the recommended way to handle self-referencing foreignkey constraints in SQL-Server? Table-Model: fiData
What is the recommended way to implement an announcments (or news) control in WPF?
What is the recommended way of handling settings for local development and the production
What is the recommended way to transfer files via FTP and manage (rename, move,
What is recommended way to keep a user configuration data in Unix/Linux? My programming
What is the recommended way of setting GET query parameters on a Restlet Request

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.