I kind of agree with Alexander - the current exception system is not useful as an error handling mechanism. Also I'm sure it's less efficient than using a return codes.
That said, I also wouldn't want Pike programming to become the nightmare Java is in terms of requiring try / catch for "all" system calls. One of the really annoying things when programming java IMHO. If a file fails to open when I call "open", return 0. If a file fails to open if I use Stdio.File("file"), there probably should be an exception thrown (since there is no other mechanism to signify an error in a constructor).
For methods requiring multiple error conditions, I much rather prefer integers to be returned. I.e:
switch(Stdio.format_filesystem("/dev/hda2", "ext5")) { case Stdio.EFILSYS: // invalid file system type break; case Stdio.EACCESS: case Stdio.EMOUNTED: // no permission or filesystem mounted break; }
etc. Similar to the concept of errno I suppose. _If_ we had a working exception system, and could write code such as this, it wouldn't be bad either:
try { Stdio.format_filesystem("/dev/hda2", "ext5"); } catch(Stdio.FileSystemTypeException e) { // invalid file system type } catch(Stdio.MountedException | Stdio.AccessException e) { // no permission or filesystem mounted }
If on the other hand we would have to write:
try { Stdio.format_filesystem("/dev/hda2", "ext5"); } catch(Stdio.FileSystemTypeException e) { // invalid file system type } catch(Stdio.AccessException e) { // no permission } catch(Stdio.MountedException e) { // filesystem mounted }
It'd be worse and we'd get the Java nightmare. If you can't handle two distinctly different exceptions in the same block of code, without resorting to some generic Exception, it's a broken system. Of course all this is purely hypothetical since we actually don't have a well defined exception system like this anyway. Given the current exceptions, I definitely vote for return codes whenever practical.
/ David Hedbor
Previous text:
2003-10-01 10:40: Subject: Re: throw or return
On Wed, Oct 01, 2003 at 12:25:02AM +0200, Martin Nilsson (saturator) @ Pike (-) developers forum wrote:
Looking through the functions in the System module it looks like a big mix between throwing an exception on error or returning the errno from the function.
In cases if we would have an exception model like in Java/C++ (where type of exception may be checked easily - say, catch only file system errors, etc) - it would make sense to use an exceptions.
But in current state, when exception returns human readable message, it makes a bit difficult task to identify the nature of exception.
Sure, in some cases exceptions would be logical ("file system is full"), but sometimes this is a bit too much, IMHO.
The ideal solution is to make it configurable - with #pragma or whatever, which model to use. Regular #defines wouldn work well in case of precompiled modules (since those will use symbols known during installation time only).
Regards, /Al
/ Brevbäraren