Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-05-18 08:29:46

0001 /*-
0002  * Copyright (c) 2009, 2020 Oracle and/or its affiliates.  All rights reserved.
0003  *
0004  * See the file LICENSE for license information.
0005  *
0006  * $Id$
0007  */
0008 
0009 #ifndef _DB_STL_EXCEPTION_H
0010 #define _DB_STL_EXCEPTION_H
0011 
0012 #include <cstring>
0013 #include <cstdlib>
0014 #include <cstdio>
0015 
0016 #include <iostream>
0017 #include <exception>
0018 
0019 #include "dbstl_common.h"
0020 
0021 START_NS(dbstl)
0022 
0023 using std::cerr;
0024 
0025 // Internally used only.
0026 void _exported throw_bdb_exception(const char *caller, int err_ret);
0027 #define COPY_CONSTRUCTOR(type) type(const type& t) : DbstlException(t){}
0028 
0029 /** \defgroup Exception_classes_group dbstl exception classes
0030 dbstl throws several types of exceptions on several kinds of errors, the
0031 exception classes form a class hiarachy. First, there is the DbstlException,
0032 which is the base class for all types of dbstl specific concrete exception 
0033 classes.
0034 DbstlException inherits from the class DbException of Berkeley DB C++ API. Since
0035 DbException class inherits from C++ STL exception base class std::exception, 
0036 you can make use of all Berkeley DB C++ and dbstl API exceptions in the same
0037 way you use the C++ std::exception class. 
0038 
0039 Besides exceptions of DbstlException and its subclasses, dbstl may also
0040 throw exceptions of DbException and its subclasses, which happens when a 
0041 Berkeley DB call failed. So you should use the same way you catch Berkeley DB
0042 C++ API exceptions when you want to catch exceptions throw by Berkeley DB 
0043 operations.
0044 
0045 When an exception occurs, dbstl initialize an local exception object on the 
0046 stack and throws the exception object, so you should catch an exception like 
0047 this:
0048 
0049 try {
0050     // dbstl operations
0051 }
0052 catch(DbstlException ex){
0053     // Exception handling
0054     throw ex; // Optionally throw ex again
0055 }
0056 
0057 @{
0058 */
0059 
0060 /// Base class of all dbstl exception classes. It is derived from Berkeley 
0061 /// DB C++ API DbException class to maintain consistency with all 
0062 /// Berkeley DB exceptions.
0063 ///
0064 class _exported DbstlException : public DbException
0065 {
0066 public:
0067     explicit DbstlException(const char *msg) : DbException(msg) {}
0068     DbstlException(const char *msg, int err) : DbException(msg, err) {}
0069     DbstlException(const DbstlException&ex) : DbException(ex) {}
0070     explicit DbstlException(int err) : DbException(err) {}
0071     DbstlException(const char *prefix, const char *msg, int err) : 
0072         DbException(prefix, msg, err) {}
0073 
0074     const DbstlException& operator=(const DbstlException&exobj)
0075     {
0076         ASSIGNMENT_PREDCOND(exobj)
0077         DbException::operator =
0078             (dynamic_cast<const DbException&>(exobj));
0079         return exobj;
0080     }
0081 
0082     virtual ~DbstlException() throw(){}
0083 };
0084 
0085 /// Failed to allocate memory because memory is not enough.
0086 class _exported NotEnoughMemoryException : public DbstlException
0087 {
0088     size_t failed_size; // The size of the failed allocation.
0089 public:
0090     NotEnoughMemoryException(const char *msg, size_t sz) 
0091         : DbstlException(msg)
0092     {
0093         failed_size = sz;
0094     }
0095 
0096      
0097     NotEnoughMemoryException(const NotEnoughMemoryException &ex)
0098         : DbstlException(ex)
0099     {
0100         this->failed_size = ex.failed_size;
0101     }
0102 };
0103 
0104 /// The iterator has inconsistent status, it is unable to be used any more.
0105 class _exported InvalidIteratorException : public DbstlException
0106 {
0107 public:
0108     InvalidIteratorException() : DbstlException("Invalid Iterator")
0109     {
0110     }
0111 
0112     explicit InvalidIteratorException(int error_code) : 
0113         DbstlException("Invalid Iterator", error_code)
0114     {
0115     }
0116     COPY_CONSTRUCTOR(InvalidIteratorException)
0117 };
0118 
0119 /// The cursor has inconsistent status, it is unable to be used any more.
0120 class _exported InvalidCursorException : public DbstlException
0121 {
0122 public:
0123     InvalidCursorException() : DbstlException("Invalid cursor")
0124     {
0125     }
0126 
0127     explicit InvalidCursorException(int error_code) : 
0128         DbstlException("Invalid cursor", error_code)
0129     {
0130     }
0131     COPY_CONSTRUCTOR(InvalidCursorException)
0132 };
0133 
0134 /// The Dbt object has inconsistent status or has no valid data, it is unable
0135 /// to be used any more.
0136 class _exported InvalidDbtException : public DbstlException
0137 {
0138 public:
0139     InvalidDbtException() : DbstlException("Invalid Dbt object")
0140     {
0141     }
0142 
0143     explicit InvalidDbtException(int error_code) : 
0144         DbstlException("Invalid Dbt object", error_code)
0145     {
0146     }
0147     COPY_CONSTRUCTOR(InvalidDbtException)
0148 };
0149 
0150 /// The assertions inside dbstl failed. The code file name and line number
0151 /// will be passed to the exception object of this class.
0152 class _exported FailedAssertionException : public DbstlException
0153 {
0154 private:
0155     char *err_msg_;
0156 public:
0157     virtual const char *what() const throw()
0158     {
0159         return err_msg_;
0160     }
0161 
0162     FailedAssertionException(const char *fname, size_t lineno, 
0163         const char *msg) : DbstlException(0)
0164     {
0165         u_int32_t sz;
0166         char *str;
0167         
0168         str = (char *)DbstlMalloc(sz = (u_int32_t)(strlen(msg) + 
0169             strlen(fname) + 128));
0170         _snprintf(str, sz, 
0171             "In file %s at line %u, %s expression failed", 
0172             fname, (unsigned int)lineno, msg);
0173         err_msg_ = str;
0174 #ifdef DEBUG
0175         fprintf(stderr, "%s", str);
0176 #endif
0177     }
0178 
0179     FailedAssertionException(const FailedAssertionException&ex) : 
0180         DbstlException(ex)
0181     {
0182         err_msg_ = (char *)DbstlMalloc((u_int32_t)
0183             strlen(ex.err_msg_) + 1);
0184         strcpy(err_msg_, ex.err_msg_);
0185     }
0186     virtual ~FailedAssertionException() throw()
0187     {
0188         free(err_msg_);
0189     }
0190 };
0191 
0192 /// There is no such key in the database. The key can't not be passed into 
0193 /// the exception instance because this class has to be a class template for
0194 /// that to work.
0195 class _exported NoSuchKeyException : public DbstlException
0196 {
0197 public:
0198     NoSuchKeyException() 
0199         : DbstlException("\nNo such key in the container.")
0200     {
0201     }
0202 
0203     COPY_CONSTRUCTOR(NoSuchKeyException)
0204 };
0205 
0206 /// Some argument of a function is invalid.
0207 class _exported InvalidArgumentException : public DbstlException 
0208 {
0209 public:
0210     explicit InvalidArgumentException(const char *errmsg) : 
0211         DbstlException(errmsg)
0212     {
0213 #ifdef DEBUG
0214         cerr<<errmsg;
0215 #endif
0216     }
0217 
0218     InvalidArgumentException(const char *argtype, const char *arg) : 
0219         DbstlException(argtype, arg, 0)
0220     {
0221 #ifdef DEBUG
0222         cerr<<"\nInvalid argument exception: "<<argtype<<"\t"<<arg;
0223 #endif
0224     }
0225 
0226     COPY_CONSTRUCTOR(InvalidArgumentException)
0227 };
0228 
0229 /// The function called is not supported in this class.
0230 class _exported NotSupportedException : public DbstlException 
0231 {
0232 public:
0233     explicit NotSupportedException(const char *str) : DbstlException(str)
0234     {
0235     }
0236 
0237     COPY_CONSTRUCTOR(NotSupportedException)
0238 };
0239 
0240 /// The function can not be called in this context or in current configurations.
0241 class _exported InvalidFunctionCall : public DbstlException 
0242 {
0243 public:
0244     explicit InvalidFunctionCall(const char *str) : DbstlException(str)
0245     {
0246 #ifdef DEBUG
0247         cerr<<"\nInvalid function call: "<<str;
0248 #endif
0249     }
0250 
0251     COPY_CONSTRUCTOR(InvalidFunctionCall)
0252 };
0253 /** @}*/
0254 #undef COPY_CONSTRUCTOR
0255 END_NS
0256 
0257 #endif //_DB_STL_EXCEPTION_H