|
-
February 10th, 2011, 10:12 AM
#1
[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.
-
February 10th, 2011, 10:20 AM
#2
Re: why should I use exception handling in my code?
-
February 10th, 2011, 10:30 AM
#3
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)
-
February 10th, 2011, 10:44 AM
#4
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.
-
February 10th, 2011, 10:44 AM
#5
Re: 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.
They are not intended to be a replacement for returning values from functions.
-
February 10th, 2011, 06:05 PM
#6
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.
-
February 12th, 2011, 03:49 PM
#7
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?
-
February 12th, 2011, 03:59 PM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|