Exception.h
Go to the documentation of this file.
00001 /*
00002  * $Id: ConfigException.h 64 2008-01-14 20:31:24Z phbaer $
00003  *
00004  * Copyright 2008 Carpe Noctem, Distributed Systems Group,
00005  * University of Kassel. All right reserved.
00006  *
00007  * The code is licensed under the Carpe Noctem Userfriendly BSD-Based
00008  * License (CNUBBL). Redistribution and use in source and binary forms,
00009  * with or without modification, are permitted provided that the
00010  * conditions of the CNUBBL are met.
00011  *
00012  * You should have received a copy of the CNUBBL along with this
00013  * software. The license is also available on our website:
00014  * http://carpenoctem.das-lab.net/license.txt
00015  */
00016 
00017 #ifndef CASTOR_EXCEPTION_H
00018 #define CASTOR_EXCEPTION_H 1
00019 
00020 #ifndef _GNU_SOURCE
00021 #  define _GNU_SOURCE
00022 #endif
00023 #include <stdio.h>
00024 
00025 #include <stdlib.h>
00026 #include <exception>
00027 #include <string>
00028 #include <iostream>
00029 #include <cstdarg>
00030 
00031 namespace castor {
00032 
00033         class Exception : public std::exception {
00034 
00035                 protected:
00036 
00037                         std::string reason;
00038 
00039                 public:
00040 
00041                         Exception(const std::string what = "unknown exception occured", ...) throw() :
00042                                 std::exception(), reason()
00043                         {
00044                                 va_list params;
00045                                 va_start(params, what);
00046                                 setReason(what, params);
00047                                 va_end(params);
00048                         }
00049 
00050                         virtual ~Exception() throw() {
00051                         }
00052                         
00053                         virtual const char *what() const throw() {
00054                                 return this->reason.c_str();
00055                         }
00056                         
00057                 protected:
00058 
00059                         void setReason(const std::string &what, va_list &args) {
00060 
00061                                 char *result = NULL;
00062 
00063                                 if (vasprintf(&result, what.c_str(), args) == -1) {
00064                                         std::cout << "Exception: Error while setting reason!" << std::endl;
00065                                 }
00066 
00067                                 this->reason = std::string(result);
00068 
00069                                 free(result);
00070                         }
00071         };
00072 }
00073 
00074 std::ostream &operator << (std::ostream &os, const castor::Exception &x);
00075 
00076 #endif /* CASTOR_EXCEPTION_H */
00077 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:39