4 ms·
"the conventional use of a null return both as an indicator of an error condition and as an actual data item" Yet, this is a thing that I have never done, nor
by sgt101 14y ago
"the conventional use of a null return both as an indicator of an error condition and as an actual data item"
Yet, this is a thing that I have never done, nor would never do. Can someone explain to me how it makes sense for this to happen in any other mode than as a genuine error? If I want to indicate an error condition I throw an exception, perhaps even one that I've written a class for. This allows the compiler to check that I've put proper handling in the code to deal with this condition. If I am stupid and I handle it with { e.printStackTrace();) well, this gets me no where... but if I listen to the compiler and write in handling code that repairs the condition then all proceeds nicely, as if the problem has been properly dealt with and all..
Or is it just me?
return null; just says "your problem f*wad" to me...
Not acceptable.
- Danieru 14y agoIn C you have no exception handling. The closest you can get is seqfualts. With a segfault the debugger can give you a backtrace at almost the exact issue point. The easiest way to get segfaults is by dereferencing NULL. In C dereferencing NULL is invalid. Thus returning NULL on failure will result in something close to what raising an exception in other languages does. In any language with real exception handling it makes much less sense, ignoring optimization.
- apaprocki 14y agoDereferencing NULL is undefined -- on AIX the page at 0x0 is readable.
- Daniel_Newby 14y agosetjmp and longjmp can be used for exception handling, if you want to lose your mind.
- tptacek 14y agoYou don't need to fault the program to take a stack trace.
- Ygg2 14y agoNull value is used to determine several things: missing value, incorrect value or unknown value (there are other semantics). A program that transforms one value into another value, may return a NULL value to denote that program processed value is missing or incorrect due to error. That was what it was in C, which had no exceptions. Your example is nice, but if you are thinking exceptions aren't a problem, keep in mind they add several branching paths in a code that seems non-branching. This hides the errors and causes all other forms of confusion. In fact I do remember an anecdotal story of a team working on a Java system of critical importance, forbidding use of exceptions in order to guarantee that code would be run smoothly. Maybe Java 8 will fix some pain points[1] of exceptions. [1]http://java8blog.com/post/37385501926/fixing-checked-exceptions-in-java-8 http://java8blog.com/post/37385501926/fixing-checked-excepti...
- deleted 14y ago[deleted]