Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a class with a IsValid() method that returns true if the object is still valid or false if not. The object starts out valid but can become invalid due to some external event.

The class has other methods with the precondition that IsValid() should return true before those methods can be invoked. What would be the proper way to handle this precondition violation? Should I use an assert or throw an exception?

My first instinct is to use an assert. However, the validity of the object changes due to an external event. If during debugging this event never happens, the assert will never get triggered. However, if it happens in a release build the assert still won't get triggered because it would've been compiled out.

Exceptions look like the answer here, but I would appreciate more opinions ...
Posted

Exception: At least that way you can log it and get some indication there is a problem, rather than just masking it and hoping it doesn't create a serious problem later on.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Mar-11 15:15pm    
Good answer, my 5; even though Assert still have some uses exception should dominate.
--SA
Go with the exception, with the other option, pain is on the horizon.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Mar-11 15:15pm    
Good answer, my 5; even though Assert still have some uses exception should dominate.
--SA
CPallini 13-Mar-11 15:45pm    
Oh asserts are invaluable for asserting!
E.g. I use assert for true preconditions (i.e. a condition that code, to my knowledge, already satisfy).
Sergey Alexandrovich Kryukov 13-Mar-11 16:50pm    
Yes, pre-condition/post-conditions are important, as well as the whole contract-based approach; always good to back it by the techniques. Good point.
--SA
Not sure I would use either - the way you describe things, it seems that the object can enter an invalid state during normal execution, so depending on how hard it is to back out of the call tree, I would consider using return values to indicate that something was not proceeding as planned.

When you use exceptions in c++, the rest of your code must also be able to deal with this – like using auto_ptr<> (or something similar) to hold references to dynamically allocated elements, and otherwise protect your program from leaking resources. If you've thought of this all along, that's great - then I guess I would go for exceptons since it probably requires less work :)

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Mar-11 15:14pm    
Best answer so far, my 5.
--SA
Espen Harlinn 13-Mar-11 15:29pm    
Thank you, SAKryukov!
Hans Dietrich 13-Mar-11 17:11pm    
Thank you, Espen. What you said is exactly right. My 5.
Espen Harlinn 13-Mar-11 17:20pm    
Thank you Hans!
Why not use both? The ASSERT macro is very useful in debug builds. Use it for pre- and postconditions. If an assert fires, either return an error token, or throw an exception.

I don't know how your set-up is, but logically an IsValid() function should return false if the instance isn't valid.

IsValid() could be a normal function, where invalid is an accepted state, or it could be a function which is only defined in debug builds, purely for debugging purposes, where invalid means a programming error. Which definition do you use?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900