Debugger break location with F# exception handling - f#

I came across this recently and wanted to check if there was something I was misunderstanding about F# exceptions. I am using Visual Studio 2012. Given the following F# code:
[<EntryPoint>]
let main argv =
try
raise (System.AccessViolationException("foo"))
printfn "Should not get here"
raise (System.AccessViolationException("foo"))
printfn "Should not get here either"
with | :? System.FormatException -> printfn "Should not get here"
This will raise and AccessViolationException which should not be caught by the with statement because the with only handles System.FormatException. I expected the debugger to be at the first raise when the exception occurs, but it is actually at the end of the with:
Now the problem is I can't see where the exception was raised from, also I can't see any information about the exception such as stacktrace. I don't think that I want to change the exception settings for individual exceptions because I would want to know the location of any unhandled exception (similar to C# or VB.NET) not when thrown.
If this is just the way that it is, then that is OK, but I just wanted to ask since I am not very familiar with F# and I wanted to make sure that I wasn't missing something. I had a look online and did not find anything about this.

Unless you want to modify your exception settings to break when that exception type is thrown (or a category of exceptions in the exception settings) then you need to make sure that your code takes care of unhandled exceptions however is most appropriate for your code.
try
raise (System.AccessViolationException("foo"))
with
| :? System.FormatException -> printfn "Should not get here"
| _ as ex ->
#if DEBUG
System.Diagnostics.Debugger.Break();
#else
printfn "Unhandled exception ('%s'): '%s'" (ex.GetType().Name) ex.Message
#endif

Related

Why does F# interactive console not consider "assert (2=3)" as wrong?

I send this line to F# interactive console in Visual Studio
assert (2=3)
To my surprise, the console does not report errors, but
 
val it : unit = ()
Similarly, if I run
printf ("hello!")
assert (2=3)
printf ("hello2")
on an REPL, I got "hellohello2" without any error messages.
How could I make the F# interactive tell me 2=3 is wrong?
Under the cover, the assert keyword translates to a method call to the Debug.Assert method from the .NET library (see the method documentaiton). This has a conditional compilation attribute [Conditional("DEBUG")], meaning that the call is only included if the symbol DEBUG is defined.
By default, this is not the case in F# Interactive. You can do that by adding --define:DEBUG to the command line parameters for fsi.exe. This will be somewhere in the options of your editor, depending on what you are using. For example, in Visual Studio, you need something like this:
EDIT: How to do something like this if you do not want to modify the command line parameters? This really depends on what exactly behaviour you want. The default behaviour of assert is that it shows a message box where you can either terminate the program or ignore the error. You could do that using:
open System.Windows.Forms
let ensure msg b =
let res =
MessageBox.Show("Assertion failed: " + msg +
"\n\nDo you want to terminate the exection? Press 'Yes' " +
"to stop or 'No' to ignore the error.", "Assertion failed",
MessageBoxButtons.YesNo)
if res = DialogResult.Yes then exit 42
ensure "Mathematics is broken" (2=3)

DirectX: can't render a model from CMO

I'm trying to draw a model using DirectX (following this tutorial: https://github.com/Microsoft/DirectXTK/wiki/Rendering-a-model?fbclid=IwAR3A0mw9rzjJHrN3mwgSb9a6oKqNgiDiAnnfkVLIIQVca9Og6cvfvscuVfE).
I've add my model.cmo file with Add existing item and following line to Game::CreateDevice()
m_model = Model::CreateFromCMO(m_d3dDevice.Get(), L"model.cmo", *m_fxFactory);
I can't build and run my project because of exception on aboved line
Unhandled exception at 0x747318A2 in directX_project.exe: Microsoft C++ exception: std::exception at memory location 0x00B3F738. occurred
I will appreciate any tips on how to resolve or debug that issue
You should enable "break on C++ exception" for the debugger as indicated in the instructions here for std::exception so you can see exactly what code triggered the exception.
Note that more recent versions of the DirectX Tool Kit provide more debug output for those kinds of failures which are probably 'file not found' or similar issue.
The DirectX Tool Kit also implements the what method for it's C++ exceptions, so you can use this code to get more detail:
try
{
m_model = Model::CreateFromCMO(m_d3dDevice.Get(), L"model.cmo", *m_fxFactory);
}
catch (std::exception& ex)
{
std::cout << ex.what();
// Do some error handling here or call throw to re-throw it
}

How to set breakpoint inside with part of try-with expression?

try
raise (InvalidOperationException())
with e ->
printfn "WTF"
I want debugger to stop at line printfn "WTF", but this does not happen. Neither in Visual Studio, nor in LinqPad.
What am I doing wrong?
When debugger in IDE is not attaching as you wish, you can call debugger break in the code to be sure it will be catched.
with e ->
System.Diagnostics.Debugger.Break()
printfn "WTF"

What is wrong with this Akka.NET code?

I spent some time searching for the Akka.NET F# API. Could not find it, even though there is good C# documentation. I found the code below, dated March 2017, which looks good, but unfortunately generates an exception when I try to run it.
Two questions:
1) What is wrong with the code below?
2) Is there online documentation for the Akka.Net F# API and if yes, where is it?
Observation: I tried several other F# Akka.NET snippets I found online and all of them generated exceptions.
The URL for the code is:
https://www.seventeencups.net/building-a-mud-with-f-sharp-and-akka-net-part-one/
And here is the code I tried to run:
open System
open Akka.Actor
open Akka.Configuration
open Akka.Remote
open Akka.FSharp
let system = System.create "system" (Configuration.defaultConfig())
type GreeterMsg =
| Hello of string
| Goodbye of string
let greeter = spawn system "greeter" <| fun mailbox ->
let rec loop() = actor {
let! msg = mailbox.Receive()
match msg with
| Hello name -> printf "Hello, %s!\n" name
| Goodbye name -> printf "Goodbye, %s!\n" name
return! loop()
}
loop()
The exception message includes the following:
System.TypeLoadException: Method 'WatchWith' in type '-ctor#270' from assembly 'Akka.FSharp, Version=1.2.3.41, Culture=neutral, PublicKeyToken=null' does not have an implementation
WatchWith method has been introduced in Akka.NET v1.3, while you're using Akka.FSharp v1.2.3. You'll need to downgrade your Akka dependency back to 1.2.3 (at this point in time Akka.FSharp is not yet available in v1.3).

F# canopy - how to use LiveHtmlReporter?

I am trying to get F# and canopy to log tests in html files.
So here it says that all I need to do is:
open configuration
open reporters
reporter <- new LiveHtmlReporter() :> IReporter
This didn't work for me. I managed to start the LiveHtmlReporter by using Chrome to start it. Now I am struggling to make it save the reports after tests are finished.
When I try to use:
reporter <- new LiveHtmlReporter(Chrome, "C:\\") :> IReporter
let liveHtmlReporter = reporter :?> LiveHtmlReporter
liveHtmlReporter.saveReportHtml #"C:\" "report"
It throws an InvalidOperationException was unhandled error at me before it get to the tests and doesn't save anything. Besides that, when the tests run - I can see only context titles, and test names are not printed - just Pass or Fail without test name.
Another thing is taking screenshot on error - it also doesn't happen.
I think I am doing wrong at the very bottom of my code. What is going wrong?
I had the same problem. This should help.
reporter <- new LiveHtmlReporter(Chrome, configuration.chromeDir) :> IReporter
let liveHtmlReporter = reporter :?> LiveHtmlReporter
liveHtmlReporter.reportPath <- Some "reports/AutomationResults"

Resources