xorp

exceptions.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/exceptions.hh,v 1.12 2009/01/05 18:30:57 jtc Exp $
00023 
00024 
00025 #ifndef __LIBXORP_EXCEPTIONS_HH__
00026 #define __LIBXORP_EXCEPTIONS_HH__
00027 
00028 #include "xorp.h"
00029 
00030 #ifdef XORP_USE_USTL
00031 #include <ustl/uexception.h>
00032 using namespace std;
00033 #else
00034 #include <exception>
00035 #endif
00036 
00037 #include <stdarg.h>
00038 
00039 #include "libxorp/c_format.hh"
00040 
00044 #define xorp_throw(_class, args...) \
00045     throw _class(__FILE__, __LINE__, args);
00046 
00047 #define xorp_throw0(_class) \
00048     throw _class(__FILE__, __LINE__);
00049 
00053 class XorpException {
00054 public:
00063     XorpException(const char* init_what, const char* file, size_t line);
00064 
00068     virtual ~XorpException();
00069 
00075     const string& what() const { return _what; }
00076 
00083     const string where() const;
00084 
00090     virtual const string why() const;
00091 
00098     string str() const;
00099 
00100 protected:
00101     string  _what;      // The type of exception
00102     const char* _file;      // The file name where exception occured
00103     size_t  _line;      // The line number where exception occured
00104 };
00105 
00109 class XorpReasonedException : public XorpException {
00110 public:
00120     XorpReasonedException(const char* init_what, const char* file, 
00121               size_t line, const string& init_why);
00122 
00128     const string why() const;
00129 
00130 protected:
00131     string _why;        // The reason for the exception
00132 };
00133 
00134 // ----------------------------------------------------------------------------
00135 // Standard XORP exceptions
00136 
00140 class InvalidString : public XorpReasonedException {
00141 public:
00142     InvalidString(const char* file, size_t line, const string& init_why = "");
00143 };
00144 
00148 class InvalidAddress : public XorpReasonedException {
00149 public:
00150     InvalidAddress(const char* file, size_t line, const string& init_why = "");
00151 };
00152 
00156 class InvalidPort : public XorpReasonedException {
00157 public:
00158     InvalidPort(const char* file, size_t line, const string& init_why = "");
00159 };
00160 
00164 class InvalidCast : public XorpReasonedException {
00165 public:
00166     InvalidCast(const char* file, size_t line, const string& init_why = "");
00167 };
00168 
00173 class InvalidBufferOffset : public XorpReasonedException {
00174 public:
00175     InvalidBufferOffset(const char* file, size_t line,
00176             const string& init_why = "");
00177 };
00178 
00183 class InvalidFamily : public XorpException {
00184 public:
00185     InvalidFamily(const char* file, size_t line, int af);
00186     const string why() const;
00187 
00188 protected:
00189     int _af;
00190 };
00191 
00195 class InvalidPacket : public XorpReasonedException {
00196 public:
00197     InvalidPacket(const char* file, size_t line, const string& init_why = "");
00198 };
00199 
00204 class InvalidNetmaskLength : public XorpException {
00205 public:
00206     InvalidNetmaskLength(const char* file, size_t line, int netmask_length);
00207     const string why() const;
00208 
00209 protected:
00210     int _netmask_length;
00211 };
00212 
00213 
00214 // ----------------------------------------------------------------------------
00215 
00220 void xorp_catch_standard_exceptions();
00221 
00229 void xorp_print_standard_exceptions();
00230 
00237 void xorp_unexpected_handler();
00238 
00244 class XorpUnexpectedHandler {
00245 public:
00246     XorpUnexpectedHandler(unexpected_handler h = xorp_unexpected_handler) {
00247     _oh = set_unexpected(h);
00248     }
00249     ~XorpUnexpectedHandler() { set_unexpected(_oh); }
00250 private:
00251     unexpected_handler _oh;
00252 };
00253 
00254 #endif // __LIBXORP_EXCEPTIONS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations