Loading ...
Sorry, an error occurred while loading the content.

Uncaught exception : why ?

Expand Messages
  • Fabrice Marchant
    Hi ! Changing : let good_f x = try g x with Not_found - h x to let bad_f x = (try g with Not_found - h) x affects the behaviour of the function : - The
    Message 1 of 3 , Jun 1, 2008
    • 0 Attachment
      Hi !

      Changing :

      let good_f x =
      try g x
      with Not_found -> h x

      to

      let bad_f x =
      (try g
      with Not_found -> h) x

      affects the behaviour of the function :

      - The former works as expected. Not_found exception raised by g is normally caught inside good_f.
      - The Not_found in the later remains uncaught ! ?

      This occurs in the too long code below I've yet tried to shorten while the odd thing remains. Can probably decrease more, sorry.

      module StringSet = Set.Make( String )
      module StringMap = Map.Make( String )

      let equal = StringMap.equal StringSet.equal

      let empty = StringMap.empty

      let id x = x

      let add_edge org aim g =
      (* Even the target vertex must have a map entry. *)
      (if StringMap.mem aim g then
      id
      else
      StringMap.add aim StringSet.empty)

      (StringMap.add
      org
      (StringSet.add aim
      (if StringMap.mem org g then
      StringMap.find org g
      else
      StringSet.empty))
      g)

      let search_loops cons_new g =
      Printf.printf "search_loops -------------\n"; flush stdout;
      StringMap.fold
      (fun start _ ->
      let rec aux org visited loop =
      StringSet.fold
      (fun e ->
      if StringSet.mem e visited then
      id
      else
      (if String.compare e start = 0 then (* Got a loop. *)
      cons_new
      else
      aux e (StringSet.add e visited)) (add_edge org e loop))
      (StringMap.find org g) in
      aux start StringSet.empty empty)
      g
      []

      let ( <<- ) f g x = f ( g x )

      let cons a d = a :: d

      (* f has a side effect.
      *)
      let en_passant f x = f x; x

      let g x = en_passant (ignore <<- (List.find (equal x)))

      (* Cons 'a' to 'd' list if 'a' doesn't belongs to 'd' according to comparison
      function 'equal'.
      This one works.
      *)
      let good_cons_new a d =
      try
      g a d
      with Not_found -> cons a d

      (* This one raise an uncaught exception : Not_found
      This does not work !
      *)
      let bad_cons_new a d =
      (try
      g a
      with Not_found -> cons a) d

      (* We build this simple loop - map of strings to set of strings - :
      "1" -> { "2" }
      "2" -> { "1" }
      *)
      let graph =
      let v1, v2 = "1", "2" in
      add_edge
      v2
      v1
      (add_edge v1 v2 empty)

      let _ =
      ignore (search_loops good_cons_new graph);
      ignore (search_loops bad_cons_new graph)

      Please why does Not_found remain uncaught using 'bad_cons_new' ?

      Fabrice
    • remi.vanicat@gmail.com
      ... It not really g that raised the exception, but the application of the function g to its argument x. So it s the g x that will raise the exception, and it s
      Message 2 of 3 , Jun 1, 2008
      • 0 Attachment
        Fabrice Marchant <fabrice.marchant@...> writes:

        > Hi !
        >
        > Changing :
        >
        > let good_f x =
        > try g x
        > with Not_found -> h x
        >
        > to
        >
        > let bad_f x =
        > (try g
        > with Not_found -> h) x
        >
        > affects the behaviour of the function :
        >
        > - The former works as expected. Not_found exception raised by g is
        > normally caught inside good_f.
        > - The Not_found in the later remains uncaught ! ?


        It not really g that raised the exception, but the application of the
        function g to its argument x. So it's the g x that will raise the
        exception, and it's g x that must be in the try with if you want it to
        catch the exception.


        --
        Rémi Vanicat
      • Fabrice Marchant
        Thanks, ... Of course ! Your explanation is perfectly clear. Regards, Fabrice
        Message 3 of 3 , Jun 2, 2008
        • 0 Attachment
          Thanks,

          > It not really g that raised the exception, but the application of the
          > function g to its argument x. So it's the g x that will raise the
          > exception, and it's g x that must be in the try with if you want it to
          > catch the exception.
          >
          >
          > --
          > Rémi Vanicat

          Of course ! Your explanation is perfectly clear.

          Regards,

          Fabrice
        Your message has been successfully submitted and would be delivered to recipients shortly.