www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Errors vs Exceptions in phobos and D

reply parabolis <parabolis softhome.net> writes:
My only exposure to the Error/Exception terminology has been 
with Java which defines an Error as something beyond a programs 
control such as the OS running out of memory when creating a new 
Object:

     Object o = new Object() // Error: Out of Memory

Exceptions are things like a negative array index that could 
have been handled by a program but were not:

     printf( "%d", array[-1] ); // Exception: Negative Index

I did not find any information in the D docs about how they work 
here.

Also I have noticed phobos has cases where Errors/Exceptions are 
badly abused:
======== phobos.std.utf ========
     void validate(char[] s)
     void validate(wchar[] s)
     void validate(dchar[] s)
         Checks to see if string is well formed or not. Throws a 

         UtfError if it is not. Use to check all untrusted input
         for correctness.
================================
Clearly this is a case where the whole point of the function is 
to answer a yes/no question about its argument. They should have 
a return type of bit.

Also if the Error/Exception semantics are similar to Java's then 
it is not normal practice to attempt to catch Errors. The 
Garbage Collector would probably be interested in an Out Of 
Memory Error as it has the power to do something about it. 
Likewise people with programs that use a significant amount of 
non-GC memory may very well be interested as well.
Jul 28 2004
next sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
Matthew noticed the same thing a few weeks ago and gave a list of Exceptions
and Errors in phobos that need the be revisted. A long thread about Errors
vs Exceptions started up, too.

"parabolis" <parabolis softhome.net> wrote in message
news:ce99tp$nqj$1 digitaldaemon.com...
 My only exposure to the Error/Exception terminology has been
 with Java which defines an Error as something beyond a programs
 control such as the OS running out of memory when creating a new
 Object:

      Object o = new Object() // Error: Out of Memory

 Exceptions are things like a negative array index that could
 have been handled by a program but were not:

      printf( "%d", array[-1] ); // Exception: Negative Index

 I did not find any information in the D docs about how they work
 here.

 Also I have noticed phobos has cases where Errors/Exceptions are
 badly abused:
 ======== phobos.std.utf ========
      void validate(char[] s)
      void validate(wchar[] s)
      void validate(dchar[] s)
          Checks to see if string is well formed or not. Throws a

          UtfError if it is not. Use to check all untrusted input
          for correctness.
 ================================
 Clearly this is a case where the whole point of the function is
 to answer a yes/no question about its argument. They should have
 a return type of bit.

 Also if the Error/Exception semantics are similar to Java's then
 it is not normal practice to attempt to catch Errors. The
 Garbage Collector would probably be interested in an Out Of
 Memory Error as it has the power to do something about it.
 Likewise people with programs that use a significant amount of
 non-GC memory may very well be interested as well.
Jul 29 2004
parent reply parabolis <parabolis softhome.net> writes:
Ben Hinkle wrote:

 Matthew noticed the same thing a few weeks ago and gave a list of Exceptions
 and Errors in phobos that need the be revisted. A long thread about Errors
 vs Exceptions started up, too.
It looks like D seems to fairly consistently be the exact opposite of Java but I will have to check that out. Thanks.
Jul 29 2004
parent reply "Matthew" <admin.hat stlsoft.dot.org> writes:
"parabolis" <parabolis softhome.net> wrote in message
news:cebfa4$1l2e$2 digitaldaemon.com...
 Ben Hinkle wrote:

 Matthew noticed the same thing a few weeks ago and gave a list of Exceptions
 and Errors in phobos that need the be revisted. A long thread about Errors
 vs Exceptions started up, too.
It looks like D seems to fairly consistently be the exact opposite of Java but I will have to check that out. Thanks.
That is, either by accident or mis-design. It's certainly not accepted by the community.
Jul 29 2004
parent reply parabolis <parabolis softhome.net> writes:
Matthew wrote:

 "parabolis" <parabolis softhome.net> wrote in message
news:cebfa4$1l2e$2 digitaldaemon.com...
 
Ben Hinkle wrote:


Matthew noticed the same thing a few weeks ago and gave a list of Exceptions
and Errors in phobos that need the be revisted. A long thread about Errors
vs Exceptions started up, too.
It looks like D seems to fairly consistently be the exact opposite of Java but I will have to check that out. Thanks.
That is, either by accident or mis-design. It's certainly not accepted by the community.
Why not change them to something less ambiguos like IdiotProgrammerAward and ActOfGod? Well I suppose I can see why some people might claim there is no difference. Still that should get everything moving in the right direction.
Jul 29 2004
next sibling parent Derek Parnell <derek psych.ward> writes:
On Fri, 30 Jul 2004 00:00:05 -0400, parabolis wrote:

 Matthew wrote:
 
 "parabolis" <parabolis softhome.net> wrote in message
news:cebfa4$1l2e$2 digitaldaemon.com...
 
Ben Hinkle wrote:


Matthew noticed the same thing a few weeks ago and gave a list of Exceptions
and Errors in phobos that need the be revisted. A long thread about Errors
vs Exceptions started up, too.
It looks like D seems to fairly consistently be the exact opposite of Java but I will have to check that out. Thanks.
That is, either by accident or mis-design. It's certainly not accepted by the community.
Why not change them to something less ambiguos like IdiotProgrammerAward and ActOfGod? Well I suppose I can see why some people might claim there is no difference. Still that should get everything moving in the right direction.
LOL. When the Amiga computer used to crash with an machine exception, we'd get a "Guru Meditition Number" displayed in big red-on-black lettering. -- Derek Melbourne, Australia 30/Jul/04 2:03:41 PM
Jul 29 2004
prev sibling parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"parabolis" <parabolis softhome.net> wrote in message
news:cech4g$23e5$1 digitaldaemon.com...
 Matthew wrote:

 "parabolis" <parabolis softhome.net> wrote in message
news:cebfa4$1l2e$2 digitaldaemon.com...

Ben Hinkle wrote:


Matthew noticed the same thing a few weeks ago and gave a list of Exceptions
and Errors in phobos that need the be revisted. A long thread about Errors
vs Exceptions started up, too.
It looks like D seems to fairly consistently be the exact opposite of Java but I will have to check that out. Thanks.
That is, either by accident or mis-design. It's certainly not accepted by the community.
Why not change them to something less ambiguos like IdiotProgrammerAward and ActOfGod? Well I suppose I can see why some people might claim there is no difference. Still that should get everything moving in the right direction.
I've suggested more than once that we cannot release 1.0 phobos with the Error/Exception debacle. Everyone seems to have responded in agreement, except Walter (unless I missed it). I just hope this is on his to-do list.
Jul 30 2004
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
parabolis wrote:

 My only exposure to the Error/Exception terminology has been with Java 
 which defines an Error as something beyond a programs control such as 
 the OS running out of memory when creating a new Object:
<snip>
 I did not find any information in the D docs about how they work here.
Yes, it needs clear defining. And consistent conventions within Phobos at least.
 Also I have noticed phobos has cases where Errors/Exceptions are badly 
 abused:
 ======== phobos.std.utf ========
     void validate(char[] s)
     void validate(wchar[] s)
     void validate(dchar[] s)
         Checks to see if string is well formed or not. Throws a
         UtfError if it is not. Use to check all untrusted input
         for correctness.
 ================================
 Clearly this is a case where the whole point of the function is to 
 answer a yes/no question about its argument.
Actually, the whole point of validate is to detect an error condition.
 They should have a return type of bit.
I think it boils down to error condition vs. normal program logic. But this is indeed an unusual case. The point is that it is an error condition for any string passed to validate to be malformed. Since wanting to read well-formed UTF text is more likely than wanting to report on whether or not a snippet of UTF text is well-formed, it is designed with the former in mind. If it were made to return a bit and never throw an exception, you'd have if (!validate(qwert)) throw new UtfError; all over your code. validate is designed with the view that you'd want to throw an exception if it fails, giving the more elegant validate(qwert);
 Also if the Error/Exception semantics are similar to Java's then it is 
 not normal practice to attempt to catch Errors. The Garbage Collector 
 would probably be interested in an Out Of Memory Error as it has the 
 power to do something about it.
<snip> For that matter, if it's out of memory, how does it allocate the OutOfMemory object itself? Is one pre-allocated, to be thrown when the memory is used up? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jul 29 2004
next sibling parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <ceb6d5$1hc8$1 digitaldaemon.com>, Stewart Gordon says...

For that matter, if it's out of memory, how does it allocate the 
OutOfMemory object itself?  Is one pre-allocated, to be thrown when the 
memory is used up?
In C++, the implementation of new is /supposed/ to guarantee that a std::bad_alloc /will/ be thrown. Microsoft Visual C++ spectacularly fails to conform to this requirement, and (I know, because I've done the experiement) allocates out of virtual memory until you run of swap space, and then eventually (...wait for it...) returns NULL! No idea what D does, but the point is you shouldn't have to throw such an exception manually. It should get thrown automatically by the runtime system when new fails. It is up to DMD to make it work /somehow/, and as such should never really be the concern of us application programmers. But of course - I'm curious too... Arcane Jill
Jul 29 2004
prev sibling parent parabolis <parabolis softhome.net> writes:
Stewart Gordon wrote:

 parabolis wrote:
 
 My only exposure to the Error/Exception terminology has been with Java 
 which defines an Error as something beyond a programs control such as 
 the OS running out of memory when creating a new Object:
<snip>
 I did not find any information in the D docs about how they work here.
Yes, it needs clear defining. And consistent conventions within Phobos at least.
 Also I have noticed phobos has cases where Errors/Exceptions are badly 
 abused:
 ======== phobos.std.utf ========
     void validate(char[] s)
     void validate(wchar[] s)
     void validate(dchar[] s)
         Checks to see if string is well formed or not. Throws a
         UtfError if it is not. Use to check all untrusted input
         for correctness.
 ================================
 Clearly this is a case where the whole point of the function is to 
 answer a yes/no question about its argument.
Actually, the whole point of validate is to detect an error condition.
Yes I understood the point of validate. I would have used something more like: bit validate(char[] s, bit throwErr = false ); Which would perform as a normal function or automatically throw an Error/Exception if explicitly requested to do so.
 Also if the Error/Exception semantics are similar to Java's then it is 
 not normal practice to attempt to catch Errors. The Garbage Collector 
 would probably be interested in an Out Of Memory Error as it has the 
 power to do something about it.
<snip> For that matter, if it's out of memory, how does it allocate the OutOfMemory object itself? Is one pre-allocated, to be thrown when the memory is used up?
That is an interesting question. A D exception object itself probably only uses about 8 bytes in memory. A more serious concern would be creating stack frames for a constructor. Which does suggest it would already have to exist.
Jul 29 2004