dtl
Category: exceptions | Component type: type |
ValidityException is thrown by classes in the DTL that derive from ValidatedObject. Such classes maintain state information that allows them to determine whether they are in a valid state or have been corrupted. Once an operation is performed in one of these self-validating classes that detects that the object is no longer in a valid state, the operation will throw a ValidityException. An exception of this type stores strings representing which method the exception was thrown in and a message describing what error occurred. The call to the constructor initializes these strings. The user can extract a stringified message with all of this information from the exception using the standard what() method.
Defined in the validate.h header file.
class MoneyAccount : public
ValidatedObject
{
private:
// if less than 0 money left, we're broke -->
object corrupted!
// (must then see MoneyAccount manager to
revalidate() or go to jail:P)
// (in this example though, once you're in
debt, your account is frozen!)
double balance;
public:
MoneyAccount(unsigned double initialDeposit) :
ValidatedObject(), balance(initialDeposit) { }
// should be able to see balance even if
account is frozen!
double GetBalance() { return balance; }
// account only valid if not in debt!
virtual bool valid()
{
// if we know the object is
invalid from a previous error,
// object is still invalid (in
this case, account still frozen)
if (!ValidatedObject::valid())
return
false;
// freeze account by invalidating
object if in debt
if (balance < 0)}
{
invalidate();
return
false;
}
else
return
true;
}
// these operations work only if the account is
not frozen
void deposit(unsigned double amt)
{
validate(); // call to
validate will throw if object not valid
balance += amt;
}
double withdraw(unsigned double amt)
{
validate(); // call to
validate will throw if object not valid
// can withdraw up to how much we
have left in the account
double amtWithdrawn = (balance
- amt >= 0 ? amt : balance);
balance -= amtWithdrawn;
return amtWithdrawn; //
return actual amount withdrawn
}
// called by creditors to make bank pay!
Account could get frozen here!
// assume bank always pays creditors even if
account is frozen
// calls to any other MoneyAccount methods
should properly discover
// any new debt and thus freeze account
void pay_creditors(unsigned double amt) {
balance -= amt; }
};
void IWillThrow()
{
// create account and then perform some
transactions
MoneyAccount acct(200.00);
acct.deposit(700.00);
acct.withdraw(550.00);
// uh oh ... now creditors take my money away,
my account is frozen
acct.pay_creditors(1000);
// now trying to do something with my frozen
account will throw ValidityException
acct.withdraw(50.00);
}
int main()
{
try
{
// call our method which
throws
IWillThrow();
}
catch (RootException &ex)
{
// can also say: cout
<< ex << endl;
// operator<<() for
RootExceptions just streams out what()
cout << ex.what()
<< endl;
}
return 0; // won't reach here ... exception
thrown above
}
Standard C++ library exception.
RootException
Member | Where defined | Description |
---|---|---|
ValidityException(const string &meth, const string &obType, Revalidation reval = NONE_TRIED) | ValidityException | Constructor which takes a specific method where the exception is being thrown from and a stringified version of the type of the object where validation failed. |
virtual const char* what() const throw() | ValidityException | Overrides behavior in RootException. Returns a C string describing the exception, including the method it was thrown in, the type of the object where validation failed, and what type of exception was thrown. Subclasses do and may override what() based on their needs. See Note [1] in class RootException. |
virtual const TCHAR* twhat() const throw() | ValidityException | Returns a pointer to a TCHAR describing the exception, including the method it was thrown in, the error message describing why it was thrown, and what type of exception was thrown. Subclasses do and may override twhat() based on their needs. See Note [1] in RootException. This is useful for returning unicode error messages. See Unicode documentation. |
friend wostream &operator<<(wostream &o, const RootException &ex) | RootException | Note that this is a friend function, and not a member. Streams out ex.twhat() to o. As twhat() is virtual, the appropriate version of that method will be called for ex. |
friend ostream &operator<<(ostream &o, const RootException &ex) | RootException | Note that this is a friend function, and not a member. Streams out ex.what() to o. As what() is virtual, the appropriate version of that method will be called for ex. |
None.
RootException,
ValidatedObject.
Copyright © 2002, Michael Gradman and Corwin Joy.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.