amuletml/amulet

How to pattern-match on specific exception type?

Closed this issue · 7 comments

Lupus commented

I'm lost on how to do that. Looking at try_ in exception.ml I figured out how to catch any exception, but if I have my own kind of exception, how can I catch specifically it?

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 *)
Lupus commented

I'll give it a try today. Thanks!

Lupus commented

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 *)
  )
Lupus commented

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
Lupus commented

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 *))