Exceptions (was Re: Structs (was RE: Record selectors))

Richard Carlsson <>
Thu Jan 16 17:31:51 CET 2003

It looks like the only real problem here is that the file API does not
offer a function that captures one of the most common uses (the
traditional "just close this file if it's open, dammit") but only the
totally generic "I'll tell you about anything that might happen during
the operation" method file.close(). Of course, you can write that one
yourself as a utility function, which might be a good idea in this

Apart from that, I don't think you can come up with any piece of code
that gives you the exact (and nontrivial, if you think about what it
does) error handling that your example contains, using fewer tests
and/or being more concisely written. (Maybe test for file==null before
reading starts?)


On Thu, 16 Jan 2003, Shawn Pearce wrote:

> 	InputStream		file = null;
> 	try
> 	{
> 		file = new FileInputStream(...);
> 		read from file
> 		file.close();
> 	} catch (IOException ioe)
> 	{
> 		if (file != null)
> 		{
> 			try
> 			{
> 				file.close();
> 			} catch (IOException ioe2)
> 			{
> 				// What do i do if i can't close the
> 				// file during an error? Assume it's
> 				// ok to ignore the second error?
> 			}
> 		}
> 		throw ioe;
> 	}
> is just an annoying mess in Java.  Exceptions are not always
> what their designers thought they would be.  I'm sure someone
> here could come up with a better format for that mess though.
> Personally, I prefer Erlang, but am told to write Java, as its
> GOL.  *sigh*
> -- 
> Shawn.

Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://user.it.uu.se/~richardc/

More information about the erlang-questions mailing list