From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.3 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 91438BC6B for ; Wed, 31 Oct 2007 06:09:37 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAEKrJ0fLENaHnmdsb2JhbACOZQIBAQcEBhEY X-IronPort-AV: E=Sophos;i="4.21,349,1188770400"; d="scan'208";a="3757751" Received: from ipmail03.adl2.internode.on.net ([203.16.214.135]) by mail2-smtp-roc.national.inria.fr with ESMTP; 31 Oct 2007 06:09:33 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJ6pJ0d5LGK+/2dsb2JhbAAM X-IronPort-AV: E=Sophos;i="4.21,349,1188743400"; d="scan'208";a="178257439" Received: from ppp121-44-98-190.lns10.syd6.internode.on.net (HELO [192.168.1.201]) ([121.44.98.190]) by ipmail03.adl2.internode.on.net with ESMTP; 31 Oct 2007 15:38:43 +1030 Subject: Re: [Caml-list] How to prevent program from crashing From: skaller To: Angela Zhu Cc: caml-list@yquem.inria.fr In-Reply-To: References: Content-Type: text/plain Date: Wed, 31 Oct 2007 16:08:41 +1100 Message-Id: <1193807321.8355.10.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.12.0 Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; syntax:01 ocaml:01 hashtbl:01 endline:01 hashtbl:01 angela:98 sourceforge:01 wrote:01 exception:01 caml-list:01 functions:01 functions:01 exceptions:01 handles:03 crashes:03 On Tue, 2007-10-30 at 23:29 -0500, Angela Zhu wrote: > > If a small syntax error in the program(written in this new language) > happens, [] > Then the whole GUI of the language crashes. You need to isolate the GUI part of your program from the part that handles errors. In particular you should be careful to undo the unfortunate tendency of various Ocaml functions to throw exceptions, and thereby 'crash' your program if you fail to handle them. For example write: let hfind msg table key = try Hashtbl.find table key with Not_found -> print_endline ("Hashtbl.find failed in " ^ msg); abort() ;; let maybe_hfind table key = try Some (Hashtbl.find table key) with Not_found -> None ;; and use one or other of these functions instead of the raw Hashtbl.find function, which can throw an exception you forget to catch and crash your program without a clear reason. Here 'hfind' will still crash your program, but hopefully help find your programming error, whereas the maybe_hfind function will never fail and uses the type system to force you to check for errors and handle them. -- John Skaller Felix, successor to C++: http://felix.sf.net