1

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

Jakub Zawiślak
  • 5,122
  • 2
  • 16
  • 21
  • 2
    Your exception handler code is working for me: https://reasonml.github.io/en/try?rrjsx=true&reason=DYUwLgBAdgrgtgIxAJwgXgmZBPCAKAM2AHsBDMAfWIIoGcsBLKAczwCIBGAGjYEpeIAbwBQECAB8IAMVINgMZCDwUBaAHwQADADpNAbmEBfA8IBStbSVaxEKXgaA – Yawar Jan 07 '20 at 16:04

2 Answers2

2

Have you checked float_of_string_opt ? This function has type string => option(float) if a error occured it will be None

Or you can do

switch (float_of_string("1,")) {
| exception (Failure(_)) => 0.0
| x => x
};

This is available if the backend OCaml is > 4.02 (BS have upgraded to 4.06 so I think it wil be available for you)

Et7f3XIV
  • 562
  • 1
  • 7
  • 20
  • Thanks for the `float_of_string_opt `. The second one didn't work fork me, I have the same uncaught exception. I have OCaml 4.07.1 – Jakub Zawiślak Jan 06 '20 at 22:45
  • 1
    `Belt.Float.fromString` is the same as `float_of_string_opt` . It is from the bucklescript standard library, and has some nice utilities in it. – JasoonS Jan 06 '20 at 23:08
  • @JakubZawiślak it is strange because it work in the playground https://reasonml.github.io/try?rrjsx=true&reason=DYUwLgBAdgrgtgIxAJwgXmvdA+AUBCAZwHcBLMAYwAsIAKAM2AHsBDMAfSfvcLGVKgBzWrDgBKMRADe+CAB8IIAB4UQABzCkmUOgDEWpYDGQha7CTggAGAHRXZCpZaWyAvgG5cuAFKEbzYVEkZFoAIgBGABpQiU9ffyZA+GCw8JixTyA (don't forget to mark as resolved or add new infos) – Et7f3XIV Jan 07 '20 at 15:55
  • I have created a demo that reproduces my issue: https://github.com/jakub-zawislak/reasonml-exception-demo – Jakub Zawiślak Jan 07 '20 at 21:47
2

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:

  • building a production package using webpack
  • serving a dev code by webpack-dev-server
  • using web sandboxes reasonml/try, codesandbox
  • running a .js file from command line using node

it works fine.

I have created an issue at BuckleScript project.

Jakub Zawiślak
  • 5,122
  • 2
  • 16
  • 21