caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Printexc cannot print some (but not all) exceptions
@ 2006-01-13 15:23 Richard Jones
  2006-01-13 15:54 ` [Caml-list] " Virgile Prevosto
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Jones @ 2006-01-13 15:23 UTC (permalink / raw)
  To: caml-list

Hi,

We have an annoying problem that sometimes Printexc seems unable to
print exceptions.  Http_client.Http_error is one such exception that
seems like it can never be printed:

Http_client.Http_error(_)			# from Printexc.to_string
(("Http_client.Http_error"), (2, "foobar"))	# from my dumper
Unix.Unix_error(0, "foobar", "baz")		# from Printexc.to_string
(("Unix.Unix_error"), 0, "foobar", "baz")	# from my dumper
Print.MyException(1, "foobar")			# from Printexc.to_string
(("Print.MyException"), 2, "foobar")		# from my dumper

(The test program which generates this is at the end of the message).

I don't understand what the common factor is which stops exceptions
from being printed, and the code in stdlib/printexc.ml seems fairly
equivalent to my "dumper" code.

Ideas?

Rich.

------------------------------------------------------------- print.ml --
(* ocamlfind ocamlopt -package extlib,netclient -linkpkg print.ml -o print *)

exception MyException of int * string;;

try
  raise (Http_client.Http_error (1, "foobar"))
with exn ->
  prerr_endline (Printexc.to_string exn);;

try
  raise (Http_client.Http_error (2, "foobar"))
with exn ->
  prerr_endline (Std.dump exn);;

try
  raise (Unix.Unix_error (Unix.E2BIG, "foobar", "baz"))
with exn ->
  prerr_endline (Printexc.to_string exn);;

try
  raise (Unix.Unix_error (Unix.E2BIG, "foobar", "baz"))
with exn ->
  prerr_endline (Std.dump exn);;

try
  raise (MyException (1, "foobar"))
with exn ->
  prerr_endline (Printexc.to_string exn);;

try
  raise (MyException (2, "foobar"))
with exn ->
  prerr_endline (Std.dump exn);;


-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Printexc cannot print some (but not all) exceptions
  2006-01-13 15:23 Printexc cannot print some (but not all) exceptions Richard Jones
@ 2006-01-13 15:54 ` Virgile Prevosto
  2006-01-13 16:36   ` Richard Jones
  0 siblings, 1 reply; 4+ messages in thread
From: Virgile Prevosto @ 2006-01-13 15:54 UTC (permalink / raw)
  To: caml-list

Hello Richard,

Le ven 13 jan 2006 15:23:34 CET, Richard Jones a écrit:
> Hi,
> 
> We have an annoying problem that sometimes Printexc seems unable to
> print exceptions.  Http_client.Http_error is one such exception that
> seems like it can never be printed:
> 
> Http_client.Http_error(_)			# from
> Printexc.to_string (("Http_client.Http_error"), (2, "foobar"))	#

> I don't understand what the common factor is which stops exceptions
> 
> ------------------------------------------------------------- print.ml
>  exception MyException of int * string;;

I'd guess that your Http_error is an exception taking one argument which
happens to be a pair, instead of two arguments (like MyException does).
Remember that, as with sum types, the two definitions:

exception OneArg of (int * string) ;;

exception TwoArg of int * string ;;

are not equivalent. Moreover, OneArg has the same behavior as Http_error:

# Printexc.to_string (OneArg (0,"foo"));;
- : string = "OneArg(_)"
# exception TwoArg of int * string ;;
exception TwoArg of int * string
# Printexc.to_string (TwoArg (0,"foo"));;
- : string = "TwoArg(0, \"foo\")"

-- 
E tutto per oggi, a la prossima volta
Virgile


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Printexc cannot print some (but not all) exceptions
  2006-01-13 15:54 ` [Caml-list] " Virgile Prevosto
@ 2006-01-13 16:36   ` Richard Jones
  2006-01-13 17:23     ` Virgile Prevosto
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Jones @ 2006-01-13 16:36 UTC (permalink / raw)
  To: Virgile Prevosto; +Cc: caml-list

On Fri, Jan 13, 2006 at 04:54:36PM +0100, Virgile Prevosto wrote:
> I'd guess that your Http_error is an exception taking one argument which
> happens to be a pair, instead of two arguments (like MyException does).
> Remember that, as with sum types, the two definitions:

Right, well spotted!  This is the definition:

exception Http_error of (int * string);;
  (* The server sent an error message. The left component of the pair is
   * the error code, the right component is the error text.
   *)

I'll resort to using Std.dump to find out what's in the exception,
although it would be nice if Printexc could look inside the tuple and
print its contents.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Printexc cannot print some (but not all) exceptions
  2006-01-13 16:36   ` Richard Jones
@ 2006-01-13 17:23     ` Virgile Prevosto
  0 siblings, 0 replies; 4+ messages in thread
From: Virgile Prevosto @ 2006-01-13 17:23 UTC (permalink / raw)
  Cc: caml-list

Le ven 13 jan 2006 16:36:29 CET, Richard Jones a écrit:
> exception Http_error of (int * string);;
>   (* The server sent an error message. The left component of the pair is
>    * the error code, the right component is the error text.
>    *)
> 
> I'll resort to using Std.dump to find out what's in the exception,
> although it would be nice if Printexc could look inside the tuple and
> print its contents.
> 

The main issue is that Printexc has no way to know that it is a tuple and
not the value of a sum type whose first non-constant constructor takes
two arguments. For Std.dump, such a value is a tuple, but it could also
have identified it with a list:

#require "extlib";;
/home/prevosto/softs/godi/lib/ocaml/pkg-lib/extlib: added to search path
#load "extlib.cma";;
# exception Wrong of (int * int);;
exception Wrong of (int * int)
#  Std.dump (Wrong (1,0));;
- : string = "[(\"Wrong\"); 1]"

I think that for a function of the standard library it is safer to make
a more conservative choice.

-- 
E tutto per oggi, a la prossima volta
Virgile


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-01-13 17:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-13 15:23 Printexc cannot print some (but not all) exceptions Richard Jones
2006-01-13 15:54 ` [Caml-list] " Virgile Prevosto
2006-01-13 16:36   ` Richard Jones
2006-01-13 17:23     ` Virgile Prevosto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).