Basic error handling facilities
authorklao
Thu, 29 Apr 2004 22:39:17 +0000
changeset 489afbdf8a3a633
parent 488 32c3548ecc2a
child 490 ceb56ff9d07f
Basic error handling facilities
(and possibly a g++-3.4.0 bug)
src/include/error.h
src/test/error_test.cc
src/test/makefile
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/include/error.h	Thu Apr 29 22:39:17 2004 +0000
     1.3 @@ -0,0 +1,64 @@
     1.4 +// -*- C++ -*- //
     1.5 +
     1.6 +#ifndef HUGO_ERROR_H
     1.7 +#define HUGO_ERROR_H
     1.8 +
     1.9 +//! \file
    1.10 +//! \brief Basic error handling (signaling) routines.
    1.11 +
    1.12 +#include <string>
    1.13 +#include <sstream>
    1.14 +
    1.15 +
    1.16 +namespace hugo {
    1.17 +
    1.18 +  /**
    1.19 +   * \brief Generic exception class.
    1.20 +   *
    1.21 +   * \todo Do we need this?
    1.22 +   *
    1.23 +   * \todo Don't we need different kind of exceptions for different kind
    1.24 +   * of errors?
    1.25 +   * Shouldn't we use <stdexcept> instead?
    1.26 +   */
    1.27 +  class Exception : public std::exception {
    1.28 +  protected:
    1.29 +    std::ostringstream buf;
    1.30 +  public:
    1.31 +    Exception() {}
    1.32 +    explicit Exception(const std::string &s) { buf << s; }
    1.33 +    Exception(const Exception &e) { buf << e.buf.str(); }
    1.34 +    virtual ~Exception() throw() {}
    1.35 +    
    1.36 +    virtual const char* what() const throw() {
    1.37 +      return buf.str().c_str();
    1.38 +    }
    1.39 +
    1.40 +    Exception& operator<<(std::string const& s) { buf << s; return *this; }
    1.41 +    Exception& operator<<(char const *s) { buf << s; return *this; }
    1.42 +    Exception& operator<<(int i) { buf << i; return *this; }
    1.43 +  };
    1.44 +
    1.45 +  /**
    1.46 +   * \brief Generic error signaling function.
    1.47 +   *
    1.48 +   * \todo Do we really need this? Is it helpful?
    1.49 +   */
    1.50 +  inline void fault(const std::string &msg) {
    1.51 +    throw Exception(msg);
    1.52 +  }
    1.53 +
    1.54 +  /**
    1.55 +   * \brief Macro for mark not yet implemented features.
    1.56 +   *
    1.57 +   * \todo Is this the right place for this? It should be used only in
    1.58 +   * modules under development.
    1.59 +   */
    1.60 +
    1.61 +# define FIXME(msg) \
    1.62 +    do { throw ::hugo::Exception("FIXME: " msg) << " (in: "    \
    1.63 +      << __FILE__ << ", " << __LINE__ << ")";                  \
    1.64 +    } while(false)
    1.65 +
    1.66 +}
    1.67 +#endif // HUGO_ERROR_H
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/test/error_test.cc	Thu Apr 29 22:39:17 2004 +0000
     2.3 @@ -0,0 +1,57 @@
     2.4 +#include <iostream>
     2.5 +
     2.6 +#include <error.h>
     2.7 +
     2.8 +using namespace hugo;
     2.9 +using std::cout;
    2.10 +using std::endl;
    2.11 +
    2.12 +void faulty_fn() {
    2.13 +  fault("This is a fault message");
    2.14 +}
    2.15 +
    2.16 +void exception_fn() {
    2.17 +  throw Exception("This is a fn throwing excpt with some args: ") 
    2.18 +    << 5 << ", " << 18;
    2.19 +}
    2.20 +
    2.21 +void unfinished_fn() {
    2.22 +  FIXME("unfinished_fn() is unfinished!");
    2.23 +}
    2.24 +
    2.25 +
    2.26 +int main() {
    2.27 +  bool no_errors = false;
    2.28 +
    2.29 +  try {
    2.30 +    cout << "Trying a faulty function\n";
    2.31 +    faulty_fn();
    2.32 +    no_errors = true;
    2.33 +    cout << "FAILED!\n";
    2.34 +  }
    2.35 +  catch(const Exception &e) {
    2.36 +    cout << "E: " << e.what() << endl;
    2.37 +  }
    2.38 +
    2.39 +  try {
    2.40 +    cout << "Trying a function throwing Exception\n";
    2.41 +    exception_fn();
    2.42 +    no_errors = true;
    2.43 +    cout << "FAILED!\n";
    2.44 +  }
    2.45 +  catch(const Exception &e) {
    2.46 +    cout << "E: " << e.what() << endl;
    2.47 +  }
    2.48 +
    2.49 +  try {
    2.50 +    cout << "Trying a function using FIXME\n";
    2.51 +    unfinished_fn();
    2.52 +    no_errors = true;
    2.53 +    cout << "FAILED!\n";
    2.54 +  }
    2.55 +  catch(const Exception &e) {
    2.56 +    cout << "E: " << e.what() << endl;
    2.57 +  }
    2.58 +
    2.59 +  return no_errors ? 1 : 0;
    2.60 +}
     3.1 --- a/src/test/makefile	Thu Apr 29 19:38:53 2004 +0000
     3.2 +++ b/src/test/makefile	Thu Apr 29 22:39:17 2004 +0000
     3.3 @@ -2,7 +2,7 @@
     3.4  CXXFLAGS += -Wall -O3 -ansi -pedantic $(INCLUDEDIRS) 
     3.5  #LEDAROOT ?= /ledasrc/LEDA-4.1
     3.6  
     3.7 -BINARIES = dijkstra_heap_test unionfind_test
     3.8 +BINARIES = dijkstra_heap_test unionfind_test error_test
     3.9  
    3.10  ifdef GCCVER
    3.11  CXX := g++-$(GCCVER)