simplified exceptions

This commit is contained in:
James Turk 2005-01-31 15:44:38 +00:00
parent b69f0daa1a
commit 87c8d327a1
2 changed files with 129 additions and 58 deletions

View File

@ -5,10 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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: // Revisions:
// $Log: exceptions.h,v $ // $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 // Revision 1.2 2005/01/27 05:24:11 cozman
// minor documentation update // minor documentation update
// //
@ -20,9 +23,13 @@
#ifndef PHOTON_EXCEPTIONS_H #ifndef PHOTON_EXCEPTIONS_H
#define PHOTON_EXCEPTIONS_H #define PHOTON_EXCEPTIONS_H
#include "types.h"
#include <string> #include <string>
#include <sstream>
#include <ostream> #include <ostream>
#define __FLOC__ __FILE__,__LINE__
namespace photon namespace photon
{ {
@ -32,7 +39,8 @@ namespace photon
// Throwable is the base exception class for Photon. // Throwable is the base exception class for Photon.
// //
// Throwable is pure virtual, and can not be used directly, Exception and // 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: // Children:
// <Exception> - Base class for all non-fatal exceptions. // <Exception> - Base class for all non-fatal exceptions.
@ -40,14 +48,25 @@ namespace photon
class Throwable class Throwable
{ {
public: public:
Throwable() throw(); // Function: Throwable
Throwable(std::string description) throw(); // 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; virtual ~Throwable() throw()=0;
std::string getDesc() const throw(); std::string getDesc() const throw();
private: private:
std::string description_; std::string description_;
std::string file_;
uint line_;
}; };
// Class: Exception // Class: Exception
@ -58,6 +77,9 @@ private:
// Exception's children should be used when a problem occurs which is // Exception's children should be used when a problem occurs which is
// recoverable. // recoverable.
// //
// Operators:
// ostream&<<
//
// See Also: // See Also:
// <Error> // <Error>
// //
@ -72,8 +94,9 @@ private:
class Exception : public Throwable class Exception : public Throwable
{ {
public: public:
Exception() throw(); Exception(std::string description = std::string(),
Exception(std::string description) throw(); std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os, friend std::ostream& operator<<(std::ostream& os,
const Exception& rhs); const Exception& rhs);
}; };
@ -82,13 +105,17 @@ public:
// ArgumentException should be thrown when an argument is passed to a function // ArgumentException should be thrown when an argument is passed to a function
// that is invalid. // that is invalid.
// //
// Operators:
// ostream&<<
//
// Parent: // Parent:
// <Exception> // <Exception>
class ArgumentException : public Exception class ArgumentException : public Exception
{ {
public: public:
ArgumentException() throw(); ArgumentException(std::string description = std::string(),
ArgumentException(std::string description) throw(); std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os, friend std::ostream& operator<<(std::ostream& os,
const ArgumentException& rhs); const ArgumentException& rhs);
}; };
@ -96,13 +123,17 @@ public:
// Class: PreconditionException // Class: PreconditionException
// PreconditionException should be thrown when a precondition is not met. // PreconditionException should be thrown when a precondition is not met.
// //
// Operators:
// ostream&<<
//
// Parent: // Parent:
// <Exception> // <Exception>
class PreconditionException : public Exception class PreconditionException : public Exception
{ {
public: public:
PreconditionException() throw(); PreconditionException(std::string description = std::string(),
PreconditionException(std::string description) throw(); std::string file = std::string(),
uint line=0) throw();
friend std::ostream& operator<<(std::ostream& os, friend std::ostream& operator<<(std::ostream& os,
const PreconditionException& rhs); const PreconditionException& rhs);
}; };
@ -111,13 +142,18 @@ public:
// RangeException should be thrown if something (such as an array bound) is out // RangeException should be thrown if something (such as an array bound) is out
// of bounds. // of bounds.
// //
// Operators:
// ostream&<<
//
// Parent: // Parent:
// <Exception> // <Exception>
class RangeException : public Exception class RangeException : public Exception
{ {
public: public:
RangeException() throw(); 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, friend std::ostream& operator<<(std::ostream& os,
const RangeException& rhs); const RangeException& rhs);
}; };
@ -126,13 +162,18 @@ public:
// ResourceException should be thrown if there is a problem accessing a // ResourceException should be thrown if there is a problem accessing a
// resource. // resource.
// //
// Operators:
// ostream&<<
//
// Parent: // Parent:
// <Exception> // <Exception>
class ResourceException : public Exception class ResourceException : public Exception
{ {
public: public:
ResourceException() throw(); 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, friend std::ostream& operator<<(std::ostream& os,
const ResourceException& rhs); const ResourceException& rhs);
}; };
@ -145,6 +186,9 @@ public:
// Errors should be used when a problem occurs which is difficult to just // Errors should be used when a problem occurs which is difficult to just
// ignore, usually more severe than exceptions. // ignore, usually more severe than exceptions.
// //
// Operators:
// ostream&<<
//
// See Also: // See Also:
// <Exception> // <Exception>
// //
@ -158,7 +202,9 @@ class Error : public Throwable
{ {
public: public:
Error() throw(); 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, friend std::ostream& operator<<(std::ostream& os,
const Error& rhs); const Error& rhs);
}; };
@ -166,13 +212,18 @@ public:
// Class: MemoryError // Class: MemoryError
// MemoryError should be thrown if there is an error allocating memory. // MemoryError should be thrown if there is an error allocating memory.
// //
// Operators:
// ostream&<<
//
// Parent: // Parent:
// <Error> // <Error>
class MemoryError : public Error class MemoryError : public Error
{ {
public: public:
MemoryError() throw(); 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, friend std::ostream& operator<<(std::ostream& os,
const MemoryError& rhs); const MemoryError& rhs);
}; };
@ -181,13 +232,18 @@ public:
// APIError should be thrown if an error occurs in code which is not part of // APIError should be thrown if an error occurs in code which is not part of
// Photon. // Photon.
// //
// Operators:
// ostream&<<
//
// Parent: // Parent:
// <Error> // <Error>
class APIError : public Error class APIError : public Error
{ {
public: public:
APIError() throw(); 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, friend std::ostream& operator<<(std::ostream& os,
const APIError& rhs); const APIError& rhs);
}; };
@ -200,12 +256,12 @@ public:
// not, throws an exception. // not, throws an exception.
// //
// An example of when to use require would be in a function that does an // 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() // void func()
// { // {
// require<PreconditionException>(pointer != NULL,"pointer must be valid"); // require<PreconditionException>(pouinter != NULL,"pouinter must be valid");
// //
// pointer->doSomething(); // pouinter->doSomething();
// } // }
// //
// Template Parameters: // Template Parameters:
@ -215,15 +271,17 @@ public:
// condition - boolean expression to be satisfied // condition - boolean expression to be satisfied
// description - description of this condition (optional parameter) // description - description of this condition (optional parameter)
template<class ExceptionT> template<class ExceptionT>
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 //definition of require template
template<class ExceptionT> template<class ExceptionT>
void require(bool condition, std::string description) void require(bool condition, std::string description,
std::string file, uint line)
{ {
if(!condition) if(!condition)
{ {
throw ExceptionT(description); throw ExceptionT(description,file,line);
} }
} }

View File

@ -5,10 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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: // Revisions:
// $Log: exceptions.cpp,v $ // $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 // Revision 1.1 2005/01/27 03:35:24 cozman
// initial import (exceptions,types, and logging,oh my!) // initial import (exceptions,types, and logging,oh my!)
// //
@ -21,81 +24,91 @@
namespace photon namespace photon
{ {
Throwable::Throwable() throw() {} Throwable::Throwable(std::string description,
Throwable::Throwable(std::string description) throw() : std::string file, uint line) throw() :
description_(description) {} description_(description), file_(file), line_(line)
Throwable::~Throwable() throw() {} {}
Throwable::~Throwable() throw()
{}
std::string Throwable::getDesc() const throw() std::string Throwable::getDesc() const throw()
{ {
return description_; std::ostringstream ss;
ss << description_;
if(!file_.empty())
{
ss << " (" << file_ << ":" << line_ << ")";
}
return ss.str();
} }
//exceptions// //exceptions//
Exception::Exception() throw() {} Exception::Exception(std::string description,
Exception::Exception(std::string description) throw() : std::string file, uint line) throw() :
Throwable(description) {} Throwable(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const Exception& rhs) 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,
ArgumentException::ArgumentException(std::string description) throw() : std::string file, uint line) throw() :
Exception(description) {} Exception(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const ArgumentException& rhs) std::ostream& operator<<(std::ostream& os, const ArgumentException& rhs)
{ {
return os << "Invalid argument exception occured. " << rhs.getDesc(); return os << "Invalid argument exception occured. " << rhs.getDesc();
} }
PreconditionException::PreconditionException() throw() {} PreconditionException::PreconditionException(std::string description,
PreconditionException::PreconditionException(std::string description) throw() : std::string file, uint line) throw() :
Exception(description) {} Exception(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const PreconditionException& rhs) 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,
RangeException::RangeException(std::string description) throw() : std::string file, uint line) throw() :
Exception(description) {} Exception(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const RangeException& rhs) 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,
ResourceException::ResourceException(std::string description) throw() : std::string file, uint line) throw() :
Exception(description) {} Exception(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const ResourceException& rhs) std::ostream& operator<<(std::ostream& os, const ResourceException& rhs)
{ {
return os << "Resource exception. " << rhs.getDesc(); return os << "Resource exception: " << rhs.getDesc();
} }
//errors// //errors//
Error::Error() throw() {} Error::Error(std::string description, std::string file, uint line) throw() :
Error::Error(std::string description) throw() : Throwable(description,file,line) {}
Throwable(description) {}
std::ostream& operator<<(std::ostream& os, const Error& rhs) 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,
MemoryError::MemoryError(std::string description) throw() : std::string file, uint line) throw() :
Error(description) {} Error(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const MemoryError& rhs) 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,
APIError::APIError(std::string description) throw() : std::string file, uint line) throw() :
Error(description) {} Error(description,file,line) {}
std::ostream& operator<<(std::ostream& os, const APIError& rhs) 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();
} }
} }