Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F# emits catch/rethrow for exception type tests #3297

Closed
mattmccutchen-microsoft opened this issue Jul 3, 2017 · 4 comments · Fixed by #12737
Closed

F# emits catch/rethrow for exception type tests #3297

mattmccutchen-microsoft opened this issue Jul 3, 2017 · 4 comments · Fixed by #12737
Labels
Area-Debug stepping, debug points, stacks and more Feature Improvement Ready Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Milestone

Comments

@mattmccutchen-microsoft
Copy link

When the F# compiler translates a try-with block, by default it seems to generate IL that catches Object and then rethrows the exception if it doesn't match any of the patterns. (Here's the relevant source code.) This has the unpleasant effect that when the Visual Studio debugger reports a user-unhandled exception, it reports it at an unrelated enclosing catch block instead of where it was originally thrown. This was previously reported on Stack Overflow.

Repro steps

Run the following code under the debugger:

exception MyException of string

[<EntryPoint>]
let main argv =
  try
    failwith "oops"
  with
    | MyException _ -> ()
  0 // return an integer exit code

Expected behavior

The exception popup appears on the "failwith" call.

Actual behavior

The exception popup appears on the "with" block, even though it doesn't catch this type of exception.

Known workarounds

In the project properties, "Build" tab, "Other flags", add --generate-filter-blocks.

Related information

  • Operating system: Windows 10 Enterprise, Version 1703, OS Build 15063.413
  • Nightly Visual F# Tools 15.4.1.17063001 installed over the following Visual Studio:
Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.14) Release
VisualStudio.15.Release/15.2.0+26430.14
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Visual F# 4.1   00369-90050-01272-AA637
Microsoft Visual F# 4.1
@mattmccutchen-microsoft
Copy link
Author

After reading this thread, maybe the answer is just "use --generate-filter-blocks (and suppress the deprecation warning) if you want". But is there a way this could be made discoverable to users the first time they see the exception popup at a catch block? An alternative would be to at least generate typed catch blocks for parity with C#.

@dsyme
Copy link
Contributor

dsyme commented Jul 3, 2017

@mattmccutchen-microsoft What are your "Just My Code" settings (Under Options --> Debug)?

@0x53A
Copy link
Contributor

0x53A commented Jul 3, 2017

I think this bugreport, and the other thread are distinct:

This one is about type-matching an exception, the other one is about exception filters. (MyException ex when ex ...).

Just for clarity, this is the equivalent C# code for the above F#:

	[EntryPoint]
	public static int main(string[] argv)
	{
		try
		{
			Unit unit = Operators.FailWith<Unit>("oops");
		}
		catch (object obj)
		{
			Exception ex = (Exception)obj;
			Program.MyException ex2 = ex as Program.MyException;
			if (ex2 == null)
			{
				throw;
			}
		}
		return 0;
	}

@cartermp cartermp added the Area-Debug stepping, debug points, stacks and more label Aug 12, 2018
@cartermp cartermp added this to the 16.0 milestone Aug 12, 2018
@cartermp cartermp modified the milestones: 16.0, 16.1 Feb 21, 2019
@cartermp cartermp modified the milestones: 16.1, 16.2 Apr 23, 2019
@cartermp cartermp modified the milestones: 16.2, Backlog Apr 30, 2019
@dsyme dsyme changed the title VS debugger reports unhandled exception at a catch block that doesn't catch it F# emits catch/rethrow for exception type tests Aug 23, 2021
@dsyme dsyme added the Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language. label Feb 16, 2022
@dsyme
Copy link
Contributor

dsyme commented Feb 16, 2022

@KathleenDollard I've labelled this as part of the "Simple F#" agenda since the experience of debugging uncaught exceptions is really important for the beginner user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Debug stepping, debug points, stacks and more Feature Improvement Ready Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants