Closed Lupus closed 4 years ago
Just add a type signature to the handler parameter of catch
Sorry, wrong button there.
catch (fun () -> (* some work *)) (fun (x : some exception) -> ...)
(* catches any exception *)
catch (fun () -> (* some work *)) (fun (x : my_exception) -> ...)
(* only catches my_exception *)
I'll give it a try today. Thanks!
So far I'm unsuccessful at solving this. That's what I tried:
module Error = begin
open Exception
type t = Decode_error of string
deriving instance typeable t
instance exception t begin
let describe_exception (Decode_error x) = "Json decode error: " ^ x
end
end
(* ... *)
Exception.catch
(fun () ->
(* do stuff *)
)
(fun (Error.Decode_error msg) ->
(* do something with msg *)
)
Ah, and the error from the compiler:
json.ml[138:26 ..141:7]: error (E2018)
No instance for exception Error.t arising from use of the expression
│
138 │ | Some (_, value) -> Exception.catch
│ ^^^^^^^^^^^^^^^
The following message has a detailed explanation: 2018.
Try 'amc explain 2018' to see it
Explicit type annotation does not change anything.
Try this instead:
let open Error in
Exception.catch
(fun () -> (* stuff *))
(fun (Decode_error msg) -> (* do something with msg *))
I'm lost on how to do that. Looking at
try_
inexception.ml
I figured out how to catch any exception, but if I have my own kind of exception, how can I catch specifically it?