BaseException

This commit is contained in:
Matej Sekoranja
2010-10-21 17:18:16 +02:00
parent c497b9b334
commit ef709eb6b9
3 changed files with 114 additions and 2 deletions

View File

@@ -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<BaseException*>(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:

View File

@@ -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

View File

@@ -0,0 +1,52 @@
/* testBaseException.cpp */
/* Author: Matej Sekoranja Date: 2010.10.18 */
#include <stddef.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "epicsException.h"
#include <epicsAssert.h>
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);
}