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 6355753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:49:39+00:00 2026-05-24T22:49:39+00:00

I needed to reraise an exception that occurs while executing an async block, after

  • 0

I needed to reraise an exception that occurs while executing an async block, after logging the exception.

When I do the following the compiler thinks that I am not calling the reraise function from within the handler. What am I doing wrong?

let executeAsync context = async {
    traceContext.Properties.Add("CorrelationId", context.CorrelationId)
    try
        do! runAsync context
        return None
    with
        | e when isCriticalException(e) ->
            logCriticalException e
            reraise()
        | e ->
            logException e
            return Some(e)
}
  • 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-05-24T22:49:41+00:00Added an answer on May 24, 2026 at 10:49 pm

    Rough! I think this is impossible, because reraise corresponds to a special IL instruction that grabs the exception from the top of the stack, but the way async expressions are compiled into a chain of continuations, I don’t think the semantics hold!

    For the same reason, the following won’t compile either:

    try
        (null:string).ToString()
    with e ->
        (fun () -> reraise())()
    

    In these situations, where I need to handle the exception outside of the actual with body, and would like to emulate reraise (that is, preserve the stack trace of the exception), I use this solution, so all together your code would look like:

    let inline reraisePreserveStackTrace (e:Exception) =
        let remoteStackTraceString = typeof<exn>.GetField("_remoteStackTraceString", BindingFlags.Instance ||| BindingFlags.NonPublic);
        remoteStackTraceString.SetValue(e, e.StackTrace + Environment.NewLine);
        raise e
    
    let executeAsync context = async {
        traceContext.Properties.Add("CorrelationId", context.CorrelationId)
        try
            do! runAsync context
            return None
        with
            | e when isCriticalException(e) ->
                logCriticalException e
                reraisePreserveStackTrace e
            | e ->
                logException e
                return Some(e)
    }
    

    Update: .NET 4.5 introduced ExceptionDispatchInfo which may allow a cleaner implementation of reraisePreserveStackTrace above.

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

Sidebar

Related Questions

I needed some simple string encryption, so I wrote the following code (with a
I needed to find all the files that contained a specific string pattern. The
I needed a generic collection or list that can fire an event when an
I needed an id field in my rails migration that auto-increments from 0 and
I needed to enumerate running processes and wondered for a while why my code
Brainstorming needed . I have a problem with Javascript libraries (jQuery, ExtJS etc.) that
I needed to create a custom select list for the user registration page that
I needed to add a crossdomain.xml file to my website so that an embeddable
I needed to rewrite lots of asp.net membership stuff since it did not meet
I needed a widget to select a TCP/UDP port, so I wrote the following:

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.