Registry Mirror
Skip to content

try catch inside of task does not compile correctly, requires return value even if exception is thrown #15419

Open

Description

@jkone27
override this.SendAsync(request, cancellationToken) =
            let wrapped = new AsyncCallableHandler(base.InnerHandler)
            let response = new HttpResponseMessage(System.Net.HttpStatusCode.Ambiguous)
                
            task {
                try
                    let! x = wrapped.CallSendAsync(request, cancellationToken)
                    
                    if x.IsSuccessStatusCode |> not then
                        response.Content <- JsonContent.Create({| Path = request.RequestUri.AbsolutePath |})
                        return response
                    else
                        return x
                with ex ->
                    failwith ex.Message
                    //return response
            }

and the result is a compiler exception

image

Expected behavior

The task CE compiles fine knowing that if an exception is thrown, no other types will be returned after

Actual behavior

When throwing or rethrowing an exception in with (catch) block, Task CE/compiler still expects a matching type T response, instead of seeing that an exception is returned and not requiring a type

Known workarounds

provide a return type after the exception is rethrown

with ex ->
                    failwith ex.Message
                    return response //workaround

Related information

Provide any related information (optional):

  • Operating system
  • NET7
  • Editing Tools : Rider

Activity

  1. added this to the Backlog milestone on Jun 16, 2023
  2. added
    Impact-Medium
    Area-Compiler-StateMachines
    and removed on Jun 19, 2023
  3. Happypig375 commented on Jul 1, 2023

    @Happypig375
    Member

    Can be solved with implicit return fsharp/fslang-suggestions#1115

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-StateMachinesBugImpact-Medium

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions