Archived

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

Solstice

Exception objects

Recommended Posts

When I started my last project, I decided to use exception handling. Currently, I have a separate class for each exception, each contributing only a string describing the error. Even with this, there seems to be much more that can be done. Any suggestions on just what should go into exception objects? -Solstice deninet.com aeris.deninet.com "...I was given three choices, the earth, the stars, or..."

Share this post


Link to post
Share on other sites
quote:
Original post by Solstice
Currently, I have a separate class for each exception, each contributing only a string describing the error.

First off, do these exception classes reflect application-specific exceptional behavior? If they don''t, use one of the exceptions described in <exception>. Second, are your exception classes derived from sd::exception?

quote:
Any suggestions on just what should go into exception objects?

Nothing else. The process of throwing an exception unwinds the stack and deallocates/destroys all objects (including partially constructed ones). The catch block should only be as granular as necessary, so that the combination of exception type and string description provide all the information necessary to handle the situation and/or notify the user/calling code.

Share this post


Link to post
Share on other sites
quote:
First off, do these exception classes reflect application-specific exceptional behavior? If they don''t, use one of the exceptions described in . Second, are your exception classes derived from sd::exception?


Well, I do define an Exception::BadStream -- Exception is a namespace -- other than that many of my exceptions are specific to the system they''re used in.

-Solstice

deninet.com
aeris.deninet.com

"...I was given three choices, the earth, the stars, or..."

Share this post


Link to post
Share on other sites
quote:
Original post by Solstice
Well, I do define an Exception::BadStream -- Exception is a namespace -- other than that many of my exceptions are specific to the system they''re used in.

-Solstice

deninet.com
aeris.deninet.com

"...I was given three choices, the earth, the stars, or..."


Publicly derive your exceptions from std::exception class. For example:
class OutOfBounds : public std::exception { };
Better yet, derive from more specific exceptions (which are themselves derived from std::exception), like logic_error or runtime_error (belive it or not, the inheritance tree goes even deeper, like range_error which is derived from runtime_error; this let''s you be very specific about what kind of exception you''re throwing).

Share this post


Link to post
Share on other sites