#include "exception.h" #include const char* exceptionHeader = "\n###"; const char* exceptionFooter = " !\n"; whereException::whereException(const char* here) : w(new char[strlen(here)+1]) { strcpy(w,here); w[strlen(here)]=0; } whereException::~whereException() { delete [] w; } ostream& operator << (ostream& s, const whereException& e) { s << exceptionHeader << "Error "; e.put(s); return (s << exceptionFooter << flush); } void whereException::put (ostream& s) const { s << " in { " << w << " }"; } whichException::whichException(const char* here) : w(new char[strlen(here)+1]) { strcpy(w,here); w[strlen(here)]=0; } whichException::~whichException() { delete [] w; } ostream& operator << (ostream& s, const whichException& e) { s << exceptionHeader << "Error "; e.put(s); return (s << exceptionFooter << flush); } void whichException::put (ostream& s) const { s << w; } outOfRange::outOfRange(const char* iName, int iValue, int lowerRange, int upperRange) : name(new char[strlen(iName)+1]), value(iValue), lower(lowerRange), upper(upperRange) { strcpy(name,iName); name[strlen(iName)]=0; } outOfRange::~outOfRange() { delete [] name; } ostream& operator << (ostream& s, const outOfRange& e) { s << exceptionHeader; e.put(s); return (s << exceptionFooter << flush); } void outOfRange::put (ostream& s) const { s << " Index " << name << "=" << value << " out of range [" << lower << "," << upper << "]"; } dimMismatch::dimMismatch(int m1, int n1, int m2, int n2) : mA(m1), nA(n1), mB(m2), nB(n2) { } ostream& operator << (ostream& s, const dimMismatch& e) { s << exceptionHeader; e.put(s); return (s << exceptionFooter << flush); } void dimMismatch::put (ostream& s) const { s << " Dimension mismatch between (" << mA << "x" << nA << ") and (" << mB << "x" << nB << ")"; } whereWhich::whereWhich (const char* here, const char* that) : whereException(here), whichException(that) { } ostream& operator << (ostream& s, const whereWhich& e) { s << exceptionHeader; e.whichException::put(s); e.whereException::put(s); return (s << exceptionFooter << flush); } whereOutOfRange::whereOutOfRange (const char* here, const char* iName, int val, int low, int up) : whereException(here), outOfRange(iName,val,low,up) { } ostream& operator << (ostream& s, const whereOutOfRange& e) { s << exceptionHeader; e.outOfRange::put(s); e.whereException::put(s); return (s << exceptionFooter << flush); } whereDimMismatch::whereDimMismatch (const char* here, int m1, int n1, int m2, int n2) : whereException(here), dimMismatch(m1,n1,m2,n2) { } ostream& operator << (ostream& s, const whereDimMismatch& e) { s << exceptionHeader; e.dimMismatch::put(s); e.whereException::put(s); return (s << exceptionFooter << flush); } void throw_whereOutOfRange(const char* w, const char* n, int i, int l, int u) { whereOutOfRange e(w,n,i,l,u); cout << e; return; } void throw_whereDimMismatch(const char* w, int m1, int n1, int m2, int n2) { whereDimMismatch e(w,m1,n1,m2,n2); cout << e; return; }