Archived

This topic is now archived and is closed to further replies.

elendil67

Exception handling?

Recommended Posts

Correct me if I''m wrong, but I think of exception handling as a replacement to this:
if(SDL_Init(SDL_INIT_VIDEO) < 0) { print_error(); }
 
Now how come I never really see it used? I''ve never seen it used in code. So why is this? Do we have a bunch of C programmers here? Thanks.

Share this post


Link to post
Share on other sites
In my mind, exceptions are messy. I feel that exceptions are like writing:

error = func_call();
if(error == 1)
goto error_is_1;
else if(error == 2)
goto error_is_2;
else if(error == 3)
goto error_is_3;
else if(error == 3)
goto error_is_3;
else if(error == 4)
goto error_is_4;
else if(error == 5)
goto error_is_5;


Obviously they aren''t that simple(as they do stack unwinding etc), but that''s how it feels to me.

Share this post


Link to post
Share on other sites
Go look at the source for an OGRE program that uses the "exampledemo" template. There is one "try" block in the whole thing, and "throw"s are sent back to there from whereever they originate from.

Now that is _clean_ exception handling.

Share this post


Link to post
Share on other sites
There has been a lot of discussion and writing about how to do exception handling well, I don't mean on this forum but amongst the designers and educators of c++. It's not easy but also there are some definitions and rules to make it easier.

The reason you don't see a lot of code which uses exception handling can be for a few reasons.

1) Exception handling hasn't been understood and hasn't been available in c++ compilers for long enough for it to have made its way into general use.

2) The difference between returning an error code and throwing an exception is so different that it means you need to change the way you design everything! There are no quick fixes to move from returning error codes to using exception safe coding techniques. The quickest 'fix', and I call it a fix but really it's not, is to replace all the if(!error) statements that you get using return codes, with try{}catch{} blocks. This looks just as messy as the old way of doing things and puts people off the whole idea (see point 1). The correct way of doing things takes a lot more effort, understanding and skill.

3) The correct way of writing exception safe code means that you hardly have any try{}catch{} blocks. The code can be designed to work correctly in the presence of exceptions and you won't even be aware of it if you don't know what you're looking for.

The books 'Exceptional C++' and 'More Exceptional C++' are excellent for improving your understanding of exception handling and general nitty gritty C++ issues as well.

[edited by - petewood on March 9, 2003 4:12:05 AM]

Share this post


Link to post
Share on other sites
Exception handling rules. Instead of returning an error code, you can return what you WANT to return and throw an exception if a problem occurs.

I don't think any of my really big programs have less than a dozen try/catch blocks.

[edited by - Nypyren on March 9, 2003 5:01:08 AM]

Share this post


Link to post
Share on other sites
If you have to check the return value of a function, you can forget doing so. You can''t "forget" a thrown exception - if you do, the program exits. This sounds like a drawback at first, but with exceptions, your program won''t enter an undefined state, and that''ll make errors much easier to find.

Share this post


Link to post
Share on other sites