🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Exception handling

Started by
4 comments, last by Mike 24 years, 5 months ago
I want to create a base class and call it something like CBaseErr. From it I would then derive CFileErr, CUsageErr, etc... I then want to catch the error. Would it be posible to do something like this: try { //some code if( bad thing happens ) { CFileErr e; throw e; } } catch( CBaseErr e ) { e.Print(); } and assuming that print is a virtual member of the base class which has been over writen by the derived class have it print out the CFileErr message? I know this question is porly worded, but I hope you see what I''m getting at. Mike
Advertisement
I *think* see what you're getting at... but first of all, why can't you just make several catch blocks for each type of specific error?

try { ... }
catch(CFileError fe) { ... }
catch(CMemoryError me) { ... }
catch(...) { printf("You suck!"); }

If you really don't want to do it that way... realize that doing it in the way you described will slice the object, and your virtual functions will NOT be called. To get around this, I suppose you could dynamically allocate (new) a CBaseError derivative, catch a pointer to it (pointers are immune to slicing), and use that... but that seems convoluted to me.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com

Edited by - mason on 1/27/00 5:18:32 PM

Edited by - mason on 1/27/00 5:20:47 PM
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
My reason for doing this was to reduce the amount of code in the catch statement. I could simply call error.print in the catch statement and if a CFileErr was thrown then it would print out the CFileErr. It would reduce that number of ifs (or the size of the case statement or the number of catches) that I would need to code.

Thats my thinking anyway. However, since (as you''ve just told me) you cannot do this in a simple manner, I''ll just continue to do it the way I have (which is multiple catch statmenets).
That will not work.
catch( CBaseErr e )
This catches a copy of CBaseErr. So if you throw CFileErr, they will grab the CBaseErr portion and create a copy and throw that copy.
I can''t remember correctly, but I recall More Effective C++ mentioned how to deal with it. I think it was something like catching a reference of CBaseErr instead of copy of CBaseErr. And then he go along to mention that it will not work cause the actualy instance went out of scope... hmmm, perhaps I will check it up at home(at work)
ahhh found it

try
{
//some code
if( bad thing happens )
throw CFileErr();
} catch( CBaseErr& e ) {
e.Print();
}


That would make e.Print() prints CFileErr''s message
Thank you for the information. I''m glad that it works.

This topic is closed to new replies.

Advertisement