dotnet / dotnet/fsharp

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

Open
#15,419 1 comment 0 reactions 0 assignees View on GitHub
Area-Compiler-StateMachines Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

```fsharp
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
```fsharp
with ex ->
failwith ex.Message
return response //workaround
```

**Related information**

Provide any related information (optional):

* Operating system
* NET7
* Editing Tools : Rider

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.