reasonbucklescript

How to catch the Failure exception raised during number parsing in ReasonML


I'm new to Reason. I want to parse a float from a string with proper exception handling.

This works:

let number = try (float_of_string("1,")) {
  | _ => 0.0;
};

But I want to do something like this:

let number = try (float_of_string("1,")) {
  | Failure(_) => 0.0;
};

Unfortunately it doesn't catch the exception. I assume that this is a Failure exception because in the developer console I see an error with this data:

[["Failure", -2], "float_of_string"]

I also tried raising that error for myself, and it was properly caught:

try (raise(Failure("test"))) {
  | Failure(_) => Js.log("caught")
};

Edit:

My code should work according to the sandbox: example on reasonml.github.io/en/try, thanks @Yawar. It also works if I run it from node instead of browser.

It seems like the exception that is thrown by that function is not from the same source as the exception that we are comparing. Maybe there is a problem with dev environment.

I have created a demo repository - Exception Demo


Solution

  • There is a a problem with moduleserve that is included in theme react-hooks (installed by bsb -init -theme react-hooks). In every other method of running code that I tried:

    it works fine.

    I have created an issue at BuckleScript project.