Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to prevent program from crashing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] How to prevent program from crashing

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 <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net