diff --git a/include/exceptions.hpp b/include/exceptions.hpp index 9171e44..b83243c 100644 --- a/include/exceptions.hpp +++ b/include/exceptions.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: exceptions.hpp,v 1.2 2005/02/16 06:58:05 cozman Exp $ +// $Id: exceptions.hpp,v 1.3 2005/02/27 05:51:59 cozman Exp $ #ifndef PHOTON_EXCEPTIONS_HPP #define PHOTON_EXCEPTIONS_HPP @@ -48,7 +48,8 @@ public: uint line=0) throw(); virtual ~Throwable() throw()=0; - std::string getDesc() const throw(); + std::string virtual what() const throw(); + friend std::ostream& operator<<(std::ostream& os, const Throwable& rhs); private: std::string description_; @@ -61,12 +62,9 @@ private: // it, and it inherits from . // Exception should be used for hard to classify exceptions. // -// Exception's children should be used when a problem occurs which is +// Exception and children should be used when a problem occurs which is // recoverable. // -// Operators: -// ostream&<< -// // See Also: // // @@ -84,17 +82,13 @@ public: Exception(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const Exception& rhs); + std::string what() const throw(); }; // Class: ArgumentException // ArgumentException should be thrown when an argument is passed to a function // that is invalid. // -// Operators: -// ostream&<< -// // Parent: // class ArgumentException : public Exception @@ -103,16 +97,12 @@ public: ArgumentException(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const ArgumentException& rhs); + std::string what() const throw(); }; // Class: PreconditionException // PreconditionException should be thrown when a precondition is not met. // -// Operators: -// ostream&<< -// // Parent: // class PreconditionException : public Exception @@ -121,60 +111,46 @@ public: PreconditionException(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const PreconditionException& rhs); + std::string what() const throw(); }; // Class: RangeException // RangeException should be thrown if something (such as an array bound) is out // of bounds. // -// Operators: -// ostream&<< -// // Parent: // class RangeException : public Exception { public: - RangeException() throw(); RangeException(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const RangeException& rhs); + std::string what() const throw(); }; // Class: ResourceException // ResourceException should be thrown if there is a problem accessing a // resource. // -// Operators: -// ostream&<< -// // Parent: // class ResourceException : public Exception { public: - ResourceException() throw(); ResourceException(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const ResourceException& rhs); + std::string what() const throw(); }; // Class: Error -// GeneralError is the base error class, all errors inherit from it, and it -// inherits from . +// Error is the base error class, all errors inherit from it, and it inherits +// from . // Error should be used for hard to classify errors. // // Errors should be used when a problem occurs which is difficult to just // ignore, usually more severe than exceptions. -// -// Operators: -// ostream&<< // // See Also: // @@ -183,57 +159,45 @@ public: // // // Children: -// // +// class Error : public Throwable { public: - Error() throw(); Error(const std::string&description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const Error& rhs); + std::string what() const throw(); }; // Class: MemoryError -// MemoryError should be thrown if there is an error allocating memory. -// -// Operators: -// ostream&<< +// MemoryError should be thrown if an error occurs while allocating memory. // // Parent: // class MemoryError : public Error { public: - MemoryError() throw(); MemoryError(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const MemoryError& rhs); -}; + std::string what() const throw(); +}; // Class: APIError // APIError should be thrown if an error occurs in code which is not part of // Photon. // -// Operators: -// ostream&<< -// // Parent: -// +// class APIError : public Error { public: - APIError() throw(); APIError(const std::string& description = std::string(), const std::string& file = std::string(), uint line=0) throw(); - friend std::ostream& operator<<(std::ostream& os, - const APIError& rhs); -}; + std::string what() const throw(); +}; } diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 33055cd..ccdaf50 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: exceptions.cpp,v 1.5 2005/02/16 06:58:26 cozman Exp $ +// $Id: exceptions.cpp,v 1.6 2005/02/27 05:52:00 cozman Exp $ #include "exceptions.hpp" @@ -22,7 +22,7 @@ Throwable::Throwable(const std::string& description, Throwable::~Throwable() throw() {} -std::string Throwable::getDesc() const throw() +std::string Throwable::what() const throw() { std::ostringstream ss; @@ -34,46 +34,51 @@ std::string Throwable::getDesc() const throw() return ss.str(); } +std::ostream& operator<<(std::ostream& os, const Throwable& rhs) +{ + return os << rhs.what(); +} + //exceptions// Exception::Exception(const std::string& description, const std::string& file, uint line) throw() : Throwable(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const Exception& rhs) +std::string Exception::what() const throw() { - return os << "General exception occured: " << rhs.getDesc(); + return "General exception occured: " + Throwable::what(); } ArgumentException::ArgumentException(const std::string& description, const std::string& file, uint line) throw() : Exception(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const ArgumentException& rhs) +std::string ArgumentException::what() const throw() { - return os << "Invalid argument exception occured. " << rhs.getDesc(); + return "Invalid argument exception occured: " + Throwable::what(); } PreconditionException::PreconditionException(const std::string& description, const std::string& file, uint line) throw() : Exception(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const PreconditionException& rhs) +std::string PreconditionException::what() const throw() { - return os << "Precondition exception occured: " << rhs.getDesc(); + return "Precondition exception occured: " + Throwable::what(); } RangeException::RangeException(const std::string& description, const std::string& file, uint line) throw() : Exception(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const RangeException& rhs) +std::string RangeException::what() const throw() { - return os << "Out-of-range exception: " << rhs.getDesc(); + return "Out-of-range exception: " + Throwable::what(); } ResourceException::ResourceException(const std::string& description, const std::string& file, uint line) throw() : Exception(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const ResourceException& rhs) +std::string ResourceException::what() const throw() { - return os << "Resource exception: " << rhs.getDesc(); + return "Resource exception: " + Throwable::what(); } //errors// @@ -81,25 +86,25 @@ std::ostream& operator<<(std::ostream& os, const ResourceException& rhs) Error::Error(const std::string& description, const std::string& file, uint line) throw() : Throwable(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const Error& rhs) +std::string Error::what() const throw() { - return os << "General error occured: " << rhs.getDesc(); + return "General error occured: " + Throwable::what(); } MemoryError::MemoryError(const std::string& description, const std::string& file, uint line) throw() : Error(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const MemoryError& rhs) +std::string MemoryError::what() const throw() { - return os << "Memory error occured: " << rhs.getDesc(); + return "Memory error occured: " + Throwable::what(); } APIError::APIError(const std::string& description, const std::string& file, uint line) throw() : Error(description,file,line) {} -std::ostream& operator<<(std::ostream& os, const APIError& rhs) +std::string APIError::what() const throw() { - return os << "Error occured within another library: " << rhs.getDesc(); + return "Error occured within another library: " + Throwable::what(); } }