From ef709eb6b9aef21bc6f4b9ed7ff5624ff0a0e9b9 Mon Sep 17 00:00:00 2001 From: Matej Sekoranja Date: Thu, 21 Oct 2010 17:18:16 +0200 Subject: [PATCH] BaseException --- pvDataApp/misc/epicsException.h | 60 +++++++++++++++++++++++++++- pvDataApp/test/Makefile | 4 ++ pvDataApp/test/testBaseException.cpp | 52 ++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 pvDataApp/test/testBaseException.cpp diff --git a/pvDataApp/misc/epicsException.h b/pvDataApp/misc/epicsException.h index f1b5e59..8a98840 100644 --- a/pvDataApp/misc/epicsException.h +++ b/pvDataApp/misc/epicsException.h @@ -1,8 +1,8 @@ /* * epicsException.hpp * - * Created on: Oct 18, 2010 - * Author: miha_vitorovic + * Created on: Oct 20, 2010 + * Author: Matej Sekoranja */ #ifndef EPICSEXCEPTION_H_ @@ -15,6 +15,62 @@ namespace epics { namespace pvData { + +class BaseException : + public std::exception { +public: + BaseException(const char* message, const char* file, int line, std::exception* cause = 0) + : m_msg(message), m_file(file), m_line(line), m_cause(cause) + { + } + + virtual ~BaseException() throw() + { + if (m_cause) delete m_cause; + } + + virtual const char* what() const throw() { return m_msg.c_str(); } + + const char* getFile() const { return m_file.c_str(); } + int getLine() const { return m_line; } + + void toString(std::string& str) { + str.append("BaseException: "); + str.append(m_msg); + str.append("\n\tat "); + str.append(m_file); + str.append(":"); + char sline[10]; + snprintf(sline, 10, "%d", m_line); + str.append(sline); + if (m_cause) + { + str.append("\ncaused by: "); + BaseException *be = dynamic_cast(m_cause); + if (be) + be->toString(str); + else + str.append(m_cause->what()); + } + } + +private: + std::string m_msg; + std::string m_file; + int m_line; + std::exception* m_cause; +}; + + +#define THROW_BASE_EXCEPTION(msg) throw new BaseException(msg, __FILE__, __LINE__) +#define THROW_BASE_EXCEPTION_CAUSE(msg, cause) throw new BaseException(msg, __FILE__, __LINE__, cause) + +/* + /// Construct with file, line info and printf-type arguments. + GenericException(const char *sourcefile, size_t line, const char *format, ...) + __attribute__ ((format (printf, 4, 5))); +*/ + /** Base Epics Exception */ class EpicsException : public std::logic_error { public: diff --git a/pvDataApp/test/Makefile b/pvDataApp/test/Makefile index cb8c0f7..4a98a8e 100644 --- a/pvDataApp/test/Makefile +++ b/pvDataApp/test/Makefile @@ -30,6 +30,10 @@ PROD_HOST += testByteBuffer testByteBuffer_SRCS += testByteBuffer.cpp testByteBuffer_LIBS += pvMisc Com +PROD_HOST += testBaseException +testBaseException_SRCS += testBaseException.cpp +testBaseException_LIBS += pvMisc + include $(TOP)/configure/RULES #---------------------------------------- # ADD RULES AFTER THIS LINE diff --git a/pvDataApp/test/testBaseException.cpp b/pvDataApp/test/testBaseException.cpp new file mode 100644 index 0000000..9a2c6be --- /dev/null +++ b/pvDataApp/test/testBaseException.cpp @@ -0,0 +1,52 @@ +/* testBaseException.cpp */ +/* Author: Matej Sekoranja Date: 2010.10.18 */ + +#include +#include +#include +#include +#include +#include "epicsException.h" + + +#include + +using namespace epics::pvData; + +void testBaseException() { + printf("testBaseException... "); + + try { + THROW_BASE_EXCEPTION("all is OK"); + } catch (BaseException *be) { + std::string str; + be->toString(str); + printf("\n\n%s\n\n", str.c_str()); + } + + try { + try { + try { + THROW_BASE_EXCEPTION("the root cause"); + } catch (BaseException *be3) { + THROW_BASE_EXCEPTION_CAUSE("exception 1", be3); + } + } catch (BaseException *be2) { + THROW_BASE_EXCEPTION_CAUSE("excepton 2", be2); + } + } catch (BaseException *be) { + std::string str; + be->toString(str); + printf("\n\n%s\n\n", str.c_str()); + delete be; + } + + printf("PASSED\n"); +} + +int main(int argc,char *argv[]) +{ + testBaseException(); + return(0); +} +