Originally Posted by
manasij7479
> AFAICT their significance has to do with calling destructors properly and the fact that constructors can't return NULL
AFAIK it also resets the stack...rolling back changes until the place where the 'catch' block is there.
So does returning a NULL pointer from a function call. Again, I'd bet money that is the whole origin of the "exception" concept.
>How meaningful is a "tidier" mistake?
I'd say..very much. I'm currently trying to write a small lisp interpreter...and exceptions made writing the REPL a joy.
What I meant was, not catching a C++ exception results in abort. In C, not checking for a NULL ptr will probably lead to a seg fault. In a sense, not catching an exception is tidier, but:
A) this is only relevant if, eg, your destructor must save some data to disk before abort.
B) if it is so relevant, why don't you just the catch the exception/check for NULL??? Not doing so when it is possible constitutes a bug IMO.
But if you do use try..catch and if(!) properly, they can accomplish much the same thing.
(Of course it would be possible to do without such mechanisms...
I think they are needed in C++, but would be meaningless in C because there is no situation whereby you cannot simply return NULL.
>In C, what you can't do with normal if (!work) error handling cascades can be done with assert()
Fine when debugging...
Yeah, that's what I meant. Exceptions can be used the same way (stick in a throw() to trace a problem). I don't actually do either thing, I just use fatal(message) functions for stuff like that, but to each their own.
but not when you have any intention of letting the program recover.
Here's a fairly common C tactic:
Code:
someThing *new_someThing () {
someThing *obj = malloc(sizeof(someThing));
[blah blah blah]
if (problem) goto fail;
[blah blah blah]
return obj;
fail:
free(obj);
reset(other stuff);
// and or
destroy(&obj);
return NULL;
}
This cleans up and returns NULL. So rather than using try...catch cascades from Inner to Outer to main(), etc, you use if (!).