caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Cannasse" <warplayer@free.fr>
To: <caml-list@inria.fr>
Subject: Re: [Caml-list] Checked exceptions and type inference
Date: Wed, 12 Mar 2003 12:24:15 +0900	[thread overview]
Message-ID: <01d601c2e846$dbeef2e0$2713f9ca@WARP> (raw)
In-Reply-To: <46D5FE1E.20DB90B0.00958B05@netscape.net>

> Perhaps I don't understand what migth be a
> 'checked exception' in ML. I am missing something?.
> can you explain it with an example

Checked exception are like in Java : the compiler warn you if you don't
catch them, and if you really don't want to do so, you have to put an
annotation on your fonction to say that it is throwing this kind of
exception.
For example

File openFile( String filename ) {
    return new File(filename);
}

compiler warn you about the IOException that can be thrown by the call to
"new File" and so you have either to ignore it :

File openFile( String filename ) {
    try {
        return new File(filename);
    } catch( IOException e ) {
        // print it if you feel like
        return null;
    }
}

or to propagate it explicitly

File openFile( String filename) throws IOException {
    return new File(filename);
}

but then the calling function of openFile will have also to decide either to
catch or to throw the IOException.
That's quite a rigorous way of handling exceptions, and you can use the
RuntimeException class to use exceptions à la OCaml.

BTW, I really dislike checked Exceptions, but I realy you want to support
them, here's some tip :

type 'a checked =
    | Result of 'a
    | Exc of exn

let check f x =
    try
        Result ( f x )
    with
        e -> Exc e

and then you can use it like this when you catch the exception :

let open_file filename =
    match check open_out filename with
    | Result x -> x
    | Exc e -> (* do something *)

or when you want to propagate it :

let open_file filename = check open_out filename

Nicolas Cannasse

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-03-12  3:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-12  3:00 Arturo Borquez
2003-03-12  3:24 ` Nicolas Cannasse [this message]
2003-03-12  3:43 ` mgushee
  -- strict thread matches above, loose matches on Subject: below --
2003-03-11 21:50 Brian Hurt
2003-03-12  5:40 ` Jacques Garrigue
2003-03-12  8:45 ` Xavier Leroy
2003-03-12 10:12   ` Stefano Zacchiroli
2003-03-12 16:34     ` Xavier Leroy
2003-03-12 17:20 ` Richard W.M. Jones
2003-03-12 20:49   ` Brian Hurt
2003-03-12 18:45 ` William Chesters

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='01d601c2e846$dbeef2e0$2713f9ca@WARP' \
    --to=warplayer@free.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).