Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Checked exceptions and type inference
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-03-12 (18:28)
From: William Chesters <williamc@p...>
Subject: [Caml-list] Checked exceptions and type inference
Brian Hurt writes:
 > Is there any research on using checked exceptions in an ML derived 
 > language?  Any plans/opinions on implementing checked exceptions in Ocaml?

Java checked exceptions are a huge pain because the checking is too
monomorphic.  Here's a trivial example:

  int numElements(Enumeration e) {
    for (int i = 0; e.hasMoreElements(); ++i) e.nextElement();

  System.out.println(numElements(new LinesInFileEnumeration("foo.txt"));

This fails because LinesInFileEnumeration wants to throw IOException,
while the Enumeration interface won't let it.  To work around this you
have to

  -- explicitly catch and re-wrap exceptions into some form acceptible
     for the interface you want to implement, at more or less every
     package boundary (not only boring but also functionally bad---
     what am I supposed to do with a wrapped exception? how can I
     tell at a higher level that it is really an IOException? and
     it really interacts badly with the debugger ...)
  or make practically every method in your program able to throw
     practically any exception.  Much real Java code ends up this way,
     with methods throwing apparently irrelevant IOExceptions and
     SQLExceptions left right and centre.  The infection spreads
     very quickly.

  or make all your exceptions RuntimeExceptions.  This is actually
     the best solution---bypass the "checked exception" system entirely.

Maybe a more polymorphic kind of exception "type checking" would be
less annoying, but I also don't think it would really achieve

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: