From 87c8d327a1b6468f981e42ea894cf0e380a868ba Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 31 Jan 2005 15:44:38 +0000 Subject: [PATCH] simplified exceptions --- include/exceptions.h | 100 ++++++++++++++++++++++++++++++++++--------- src/exceptions.cpp | 87 +++++++++++++++++++++---------------- 2 files changed, 129 insertions(+), 58 deletions(-) diff --git a/include/exceptions.h b/include/exceptions.h index fd25bcc..0fcd982 100644 --- a/include/exceptions.h +++ b/include/exceptions.h @@ -5,10 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: exceptions.h,v 1.2 2005/01/27 05:24:11 cozman Exp $ +// $Id: exceptions.h,v 1.3 2005/01/31 15:44:38 cozman Exp $ // // Revisions: // $Log: exceptions.h,v $ +// Revision 1.3 2005/01/31 15:44:38 cozman +// simplified exceptions +// // Revision 1.2 2005/01/27 05:24:11 cozman // minor documentation update // @@ -20,9 +23,13 @@ #ifndef PHOTON_EXCEPTIONS_H #define PHOTON_EXCEPTIONS_H +#include "types.h" #include +#include #include +#define __FLOC__ __FILE__,__LINE__ + namespace photon { @@ -32,7 +39,8 @@ namespace photon // Throwable is the base exception class for Photon. // // Throwable is pure virtual, and can not be used directly, Exception and -// Error are available for non-specific exceptions. +// Error are available for non-specific exceptions. All exceptions have the +// same interface as Throwable. // // Children: // - Base class for all non-fatal exceptions. @@ -40,14 +48,25 @@ namespace photon class Throwable { public: - Throwable() throw(); - Throwable(std::string description) throw(); + // Function: Throwable + // Constructor for a Throwable object, can specify as little or as much + // information as you'd like when throwing. + // + // Parameters: + // description - description of why exception was thrown [default: empty] + // file - name of file where exception was thrown [default: empty] + // line - line in file where exception was thrown [default: 0] + Throwable(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); virtual ~Throwable() throw()=0; std::string getDesc() const throw(); private: std::string description_; + std::string file_; + uint line_; }; // Class: Exception @@ -58,6 +77,9 @@ private: // Exception's children should be used when a problem occurs which is // recoverable. // +// Operators: +// ostream&<< +// // See Also: // // @@ -72,8 +94,9 @@ private: class Exception : public Throwable { public: - Exception() throw(); - Exception(std::string description) throw(); + Exception(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const Exception& rhs); }; @@ -82,13 +105,17 @@ public: // ArgumentException should be thrown when an argument is passed to a function // that is invalid. // +// Operators: +// ostream&<< +// // Parent: // class ArgumentException : public Exception { public: - ArgumentException() throw(); - ArgumentException(std::string description) throw(); + ArgumentException(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const ArgumentException& rhs); }; @@ -96,13 +123,17 @@ public: // Class: PreconditionException // PreconditionException should be thrown when a precondition is not met. // +// Operators: +// ostream&<< +// // Parent: // class PreconditionException : public Exception { public: - PreconditionException() throw(); - PreconditionException(std::string description) throw(); + PreconditionException(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const PreconditionException& rhs); }; @@ -111,13 +142,18 @@ public: // 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(std::string description) throw(); + RangeException(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const RangeException& rhs); }; @@ -126,13 +162,18 @@ public: // ResourceException should be thrown if there is a problem accessing a // resource. // +// Operators: +// ostream&<< +// // Parent: // class ResourceException : public Exception { public: ResourceException() throw(); - ResourceException(std::string description) throw(); + ResourceException(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const ResourceException& rhs); }; @@ -144,6 +185,9 @@ public: // // Errors should be used when a problem occurs which is difficult to just // ignore, usually more severe than exceptions. +// +// Operators: +// ostream&<< // // See Also: // @@ -158,7 +202,9 @@ class Error : public Throwable { public: Error() throw(); - Error(std::string description) throw(); + Error(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const Error& rhs); }; @@ -166,13 +212,18 @@ public: // Class: MemoryError // MemoryError should be thrown if there is an error allocating memory. // +// Operators: +// ostream&<< +// // Parent: // class MemoryError : public Error { public: MemoryError() throw(); - MemoryError(std::string description) throw(); + MemoryError(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const MemoryError& rhs); }; @@ -181,13 +232,18 @@ public: // 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(std::string description) throw(); + APIError(std::string description = std::string(), + std::string file = std::string(), + uint line=0) throw(); friend std::ostream& operator<<(std::ostream& os, const APIError& rhs); }; @@ -200,12 +256,12 @@ public: // not, throws an exception. // // An example of when to use require would be in a function that does an -// operation to a certain pointer: +// operation to a certain pouinter: // void func() // { -// require(pointer != NULL,"pointer must be valid"); +// require(pouinter != NULL,"pouinter must be valid"); // -// pointer->doSomething(); +// pouinter->doSomething(); // } // // Template Parameters: @@ -215,15 +271,17 @@ public: // condition - boolean expression to be satisfied // description - description of this condition (optional parameter) template -void require(bool condition, std::string description=""); +void require(bool condition, std::string description = std::string(), + std::string file = std::string(), uint line=0); //definition of require template template -void require(bool condition, std::string description) +void require(bool condition, std::string description, + std::string file, uint line) { if(!condition) { - throw ExceptionT(description); + throw ExceptionT(description,file,line); } } diff --git a/src/exceptions.cpp b/src/exceptions.cpp index 1f9f781..55488f4 100644 --- a/src/exceptions.cpp +++ b/src/exceptions.cpp @@ -5,10 +5,13 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: exceptions.cpp,v 1.1 2005/01/27 03:35:24 cozman Exp $ +// $Id: exceptions.cpp,v 1.2 2005/01/31 15:44:38 cozman Exp $ // // Revisions: // $Log: exceptions.cpp,v $ +// Revision 1.2 2005/01/31 15:44:38 cozman +// simplified exceptions +// // Revision 1.1 2005/01/27 03:35:24 cozman // initial import (exceptions,types, and logging,oh my!) // @@ -21,81 +24,91 @@ namespace photon { -Throwable::Throwable() throw() {} -Throwable::Throwable(std::string description) throw() : - description_(description) {} -Throwable::~Throwable() throw() {} +Throwable::Throwable(std::string description, + std::string file, uint line) throw() : + description_(description), file_(file), line_(line) +{} + +Throwable::~Throwable() throw() +{} + std::string Throwable::getDesc() const throw() { - return description_; + std::ostringstream ss; + + ss << description_; + if(!file_.empty()) + { + ss << " (" << file_ << ":" << line_ << ")"; + } + return ss.str(); } //exceptions// -Exception::Exception() throw() {} -Exception::Exception(std::string description) throw() : - Throwable(description) {} +Exception::Exception(std::string description, + std::string file, uint line) throw() : + Throwable(description,file,line) {} std::ostream& operator<<(std::ostream& os, const Exception& rhs) { - return os << "General exception occured. " << rhs.getDesc(); + return os << "General exception occured: " << rhs.getDesc(); } -ArgumentException::ArgumentException() throw() {} -ArgumentException::ArgumentException(std::string description) throw() : - Exception(description) {} +ArgumentException::ArgumentException(std::string description, + std::string file, uint line) throw() : + Exception(description,file,line) {} std::ostream& operator<<(std::ostream& os, const ArgumentException& rhs) { return os << "Invalid argument exception occured. " << rhs.getDesc(); } -PreconditionException::PreconditionException() throw() {} -PreconditionException::PreconditionException(std::string description) throw() : - Exception(description) {} +PreconditionException::PreconditionException(std::string description, + std::string file, uint line) throw() : + Exception(description,file,line) {} std::ostream& operator<<(std::ostream& os, const PreconditionException& rhs) { - return os << "Precondition exception occured. " << rhs.getDesc(); + return os << "Precondition exception occured: " << rhs.getDesc(); } -RangeException::RangeException() throw() {} -RangeException::RangeException(std::string description) throw() : - Exception(description) {} +RangeException::RangeException(std::string description, + std::string file, uint line) throw() : + Exception(description,file,line) {} std::ostream& operator<<(std::ostream& os, const RangeException& rhs) { - return os << "Out-of-range exception. " << rhs.getDesc(); + return os << "Out-of-range exception: " << rhs.getDesc(); } -ResourceException::ResourceException() throw() {} -ResourceException::ResourceException(std::string description) throw() : - Exception(description) {} +ResourceException::ResourceException(std::string description, + std::string file, uint line) throw() : + Exception(description,file,line) {} std::ostream& operator<<(std::ostream& os, const ResourceException& rhs) { - return os << "Resource exception. " << rhs.getDesc(); + return os << "Resource exception: " << rhs.getDesc(); } //errors// -Error::Error() throw() {} -Error::Error(std::string description) throw() : - Throwable(description) {} +Error::Error(std::string description, std::string file, uint line) throw() : + Throwable(description,file,line) {} std::ostream& operator<<(std::ostream& os, const Error& rhs) { - return os << "General error occured. " << rhs.getDesc(); + return os << "General error occured: " << rhs.getDesc(); } -MemoryError::MemoryError() throw() {} -MemoryError::MemoryError(std::string description) throw() : - Error(description) {} +MemoryError::MemoryError(std::string description, + std::string file, uint line) throw() : + Error(description,file,line) {} std::ostream& operator<<(std::ostream& os, const MemoryError& rhs) { - return os << "Memory error occured. " << rhs.getDesc(); + return os << "Memory error occured: " << rhs.getDesc(); } -APIError::APIError() throw() {} -APIError::APIError(std::string description) throw() : - Error(description) {} +APIError::APIError(std::string description, + std::string file, uint line) throw() : + Error(description,file,line) {} std::ostream& operator<<(std::ostream& os, const APIError& rhs) { - return os << "Error occured within another library. " << rhs.getDesc(); + return os << "Error occured within another library: " << rhs.getDesc(); } }