LOOS  v2.3.2
exceptions.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2009, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 #if !defined(LOOS_EXCEPTIONS_HPP)
24 #define LOOS_EXCEPTIONS_HPP
25 
26 
27 #include <sstream>
28 #include <exception>
29 #include <string>
30 #include <loos_defs.hpp>
31 
32 namespace loos {
33 
34 
35  // Forward decl's
36  class Atom;
37  std::ostream& operator<<(std::ostream&, const Atom&);
38 
40  class LOOSError : public std::exception {
41  protected:
42  std::string _msg;
43  public:
44  explicit LOOSError() : _msg("LOOS Error") { }
45  explicit LOOSError(const std::string& arg) : _msg(arg) { }
46  explicit LOOSError(const Atom& a, const std::string& arg) {
47  std::stringstream ss;
48  ss << a << std::endl << arg;
49  _msg = ss.str();
50  }
51 
52  virtual ~LOOSError() throw() {};
53  virtual const char* what(void) const throw() { return(_msg.c_str()); }
54  };
55 
57  class OptionsError : public LOOSError
58  {
59  public:
60  explicit OptionsError(const std::string& arg) : LOOSError(arg) { }
61  };
62 
64  class ParseError : public LOOSError {
65  public:
66  explicit ParseError(const std::string& arg) : LOOSError(arg) { }
67  };
68 
69 
71  class UnsetProperty : public LOOSError {
72  public:
73  UnsetProperty() : LOOSError("Attempting to access an unset atom property") {}
74  UnsetProperty(const std::string& p) : LOOSError(p) {}
75  UnsetProperty(const Atom& a, const std::string& p) : LOOSError(a, p) {}
76  };
77 
78 
80 
84  struct XDRDataSizeError : public LOOSError {
85  XDRDataSizeError() : LOOSError("XDR data size error") {}
86  XDRDataSizeError(const std::string& s) : LOOSError(s) {}
87  };
88 
89 
91  class NumericalError : public LOOSError {
92  public:
93  explicit NumericalError(const std::string& arg, const int info) {
94  std::stringstream ss;
95  ss << arg << ", info = " << info;
96  _msg = ss.str();
97  }
98  explicit NumericalError(const std::string& arg) : LOOSError(arg) { }
99  };
100 
101 
103 
110  class FileError : public LOOSError {
111  protected:
112  std::string _operation;
113  std::string _filename;
114  int _errcode;
115 
116  public:
117  FileError(const std::string& op) : LOOSError("Error while " + op), _operation(op) {}
118 
119  FileError(const std::string& op, const std::string& fname)
120  : LOOSError("Error while " + op + " " + fname),
121  _operation(op), _filename(fname)
122  {}
123 
124  FileError(const std::string& op,
125  const std::string& fname,
126  const std::string& msg)
127  : LOOSError("Error while " + op + " " + fname + msg),
128  _operation(op),
129  _filename(fname)
130  {}
131 
132  FileError(const std::string& op,
133  const std::string& fname,
134  const std::string& msg,
135  const int err)
136  : LOOSError("Error while " + op + " " + fname + msg),
137  _operation(op),
138  _filename(fname),
139  _errcode(err)
140  {}
141 
142 
143 
145  std::string operation() const throw() { return(_operation); }
146 
148  std::string filename() const throw() { return(_filename); }
149 
151  int errorCode() const { return(_errcode); }
152 
154  void errorCode(const int i) { _errcode = i; }
155 
156  ~FileError() throw() {}
157  };
158 
160 
170  class FileOpenError : public FileError {
171  public:
172  FileOpenError() : FileError("opening") { }
173  FileOpenError(const std::string& fname) : FileError("opening", fname) {}
174  FileOpenError(const std::string& fname, const std::string& msg) : FileError("opening", fname, '\n' + msg) {}
175  FileOpenError(const std::string& fname, const std::string& msg, const int err) : FileError("opening", fname, '\n' + msg, err) {}
176  };
177 
178 
180  class FileReadError : public FileError {
181  public:
182  FileReadError() : FileError("reading from") { }
183  FileReadError(const std::string& fname) : FileError("reading from", fname) {}
184  FileReadError(const std::string& fname, const std::string& msg) : FileError("reading from", fname, '\n' + msg) {}
185  FileReadError(const std::string& fname, const std::string& msg, const int err) : FileError("reading from", fname, '\n' + msg, err) {}
186  };
187 
188 
191  protected:
192  uint _lineno;
193  std::string _msg;
194  public:
195  FileReadErrorWithLine(const uint ln)
196  : FileReadError("reading"), _lineno(ln)
197  { init(); }
198 
199  FileReadErrorWithLine(const std::string& fname, const uint ln)
200  : FileReadError("reading ", fname), _lineno(ln)
201  { init(); }
202 
203 
204  FileReadErrorWithLine(const std::string& fname, const std::string& msg, const uint ln)
205  : FileReadError("reading ", fname), _lineno(ln), _msg(msg)
206  { init(); }
207 
209  uint lineNumber() const throw() { return(_lineno); }
210 
211  ~FileReadErrorWithLine() throw() {}
212 
213  private:
214  void init() {
215  std::ostringstream oss;
216 
217  oss << FileReadError::_msg << " at line " << _lineno << std::endl << _msg;
218  FileReadError::_msg = oss.str();
219  }
220 
221  };
222 
223 
224 
226  class FileWriteError : public FileError {
227  public:
228  FileWriteError() : FileError("writing to") { }
229  FileWriteError(const std::string& fname) : FileError("writing to", fname) {}
230  FileWriteError(const std::string& fname, const std::string& msg) : FileError("writing to", fname, '\n' + msg) {}
231  };
232 
233 
234 
235 
236 };
237 
238 #endif
Errors related to File I/O.
Definition: exceptions.hpp:110
Errors that occur while reading a file.
Definition: exceptions.hpp:180
Error while opening a file.
Definition: exceptions.hpp:170
Exception in options.
Definition: exceptions.hpp:57
Basic Atom class for handling atom properties.
Definition: Atom.hpp:50
Exception when trying to use an unset Atom property.
Definition: exceptions.hpp:71
std::string filename() const
File that had the problem (or "stream" if not a file)
Definition: exceptions.hpp:148
Generic LOOS exception.
Definition: exceptions.hpp:40
int errorCode() const
The error code that may have been generated.
Definition: exceptions.hpp:151
Exception indicating internal XDR error.
Definition: exceptions.hpp:84
Exception caused by a blas/atlas error.
Definition: exceptions.hpp:91
uint lineNumber() const
The line number that caused the problem.
Definition: exceptions.hpp:209
Exception when parsing input data.
Definition: exceptions.hpp:64
Namespace for most things not already encapsulated within a class.
Errors while writing to files.
Definition: exceptions.hpp:226
void errorCode(const int i)
Sets the error code.
Definition: exceptions.hpp:154
std::string operation() const
What operation was involved (e.g. reading, writing. etc)
Definition: exceptions.hpp:145
Errors that occur while reading a text file (where lines are tracked)
Definition: exceptions.hpp:190