[RESOLVED] why should I use exception handling in my code?
I've read about exception handling, but I don't understand why they are better than the old way.
is exception handling faster?
why should I do this:
Code:
void func(int x, int y)
{
if (x != y) throw "they are equal";
else cout << x - y;
}
rather than this:
Code:
bool func(int x, int y)
{
if (x != y) return false;
cout << x - y;
return true;
}
I would use the second variant, because I write less when calling the function.
So, why is exception handling better?
P.S. the function is not supposed to be useful, it's just for showing the difference.
Re: why should I use exception handling in my code?
Re: why should I use exception handling in my code?
Exception handling is not faster, it's slower, it has to unwind the stack when they are thrown. But it's not significant, you'll never notice a difference unless you do something dumb. Exceptions are supposed to be error cases, which happen infrequently. If you encounter an error once in a million cycles (a high rate,) you'll maybe loose a couple of milliseconds in that time. Exception are more standard in the current paradigm, although a lot of people still use the error return codes (I do usually :P)
Re: why should I use exception handling in my code?
Better control of where you want program control to go if an error is encountered. If you have a sequence of function calls you want to make, especially if some of them are nested and you want control to return to the same spot if any of them fail, you can wrap the whole thing up in a try/catch block without having to check and propagate the return codes of all the functions and nested functions you call.
You can pass more detailed information about what went wrong back to the caller.
Re: why should I use exception handling in my code?
Quote:
I've read about exception handling, but I don't understand why they are better than the old way.
They are not intended to be a replacement for returning values from functions.
Re: why should I use exception handling in my code?
thanks for all the info.
And Chris_F, thanks a lot for the link. it was very useful.
Re: [RESOLVED] why should I use exception handling in my code?
by the way, a few more questions:
a. is there a point to do something like this:
Code:
try
{
char* b = new b;
}
catch (...)
{
//here you should not be able to do anything unless you can release some memory from other places... if you can.
}
b. should I use a try - catch block for something like this:
?
c. what happens if I use the STL's string class, but it cannot allocate memory? I assume that the program would crash... and if so, I don't understand why a raw string must be allocated using exception handlers: the program would crash anyway. Am I mistaken somewhere?
Re: [RESOLVED] why should I use exception handling in my code?
Generally speaking if you fail to allocate memory, the application will crash and there isn't much you can do about it. I know some garbage collectors will do an emergency collection on a failure to allocate memory to try and prevent a crash. Not really applicable unless you are using some kind of GC library.