An exception is a class. Exceptions can be thrown and caught by methods. If a method throws an exception, it must be declared: returnType methodName( arguments) throw (ExceptionName)
class TimeStampException { public: TimeStampException(); ![]() } |
void TimeStamp::update(void) throw (TimeStampException) {
using namespace std;
struct timeval tv;
struct timezone tz;
try {
int retVal = gettimeofday(&tv, &tz);
if (retVal == -1) {
cerr « "TimeStamp::updateTimeStamp(): "
« "could not get system time" « endl;
throw TimeStampException();
}
} catch (...) {
cerr « "TimeStamp::updateTimeStamp(): "
« "could not get system time" « endl;
throw TimeStampException();
}
timestamp = tv.tv_sec;
}
Leo Liberti 2008-01-12