Exception.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #include "core/basics/Exception.h"
  7. #include <sstream>
  8. #include <iostream>
  9. using namespace std;
  10. namespace NICE {
  11. Exception::Exception(const std::string& _message, const exception& _cause)
  12. : filename("unknown"), line(0), message(_message) {
  13. // NOTE for some (?) causing exceptions, the following code is
  14. // the ONLY working variant known -- DO NOT CHANGE!
  15. const std::string& temp = _cause.what();
  16. for (unsigned int i = 0; i < temp.size(); i++) {
  17. cause += temp[i];
  18. }
  19. }
  20. Exception::Exception(const char* _filename, const int _line,
  21. const std::string& _message, const exception& _cause)
  22. : filename(_filename), line(_line), message(_message) {
  23. // NOTE for some (?) causing exceptions, the following code is
  24. // the ONLY working variant known -- DO NOT CHANGE!
  25. const std::string& temp = _cause.what();
  26. for (unsigned int i = 0; i < temp.size(); i++) {
  27. cause += temp[i];
  28. }
  29. }
  30. Exception::~Exception() throw() {
  31. }
  32. const char* Exception::what() const throw() {
  33. try {
  34. std::stringstream s;
  35. s << "Exception " << name()
  36. << " in file " << filename
  37. << " line " << line
  38. << " with message:" << std::endl << " " << message;
  39. if (cause.size() > 0) {
  40. s << std::endl << " caused by: " << cause;
  41. }
  42. whatBuffer = s.str();
  43. return whatBuffer.c_str();
  44. } catch(...) {
  45. return "Exception: exception while formating what()-string";
  46. }
  47. }
  48. const char* Exception::name() const throw() {
  49. return "Exception";
  50. }
  51. } // namespace