What type of exception should I throw?

后端 未结 5 1173
难免孤独
难免孤独 2020-12-08 03:40

After going through some links on exception handling (1, 2, and 3), I know that C++ programs can throw pretty much anything as exceptions (int, char*

5条回答
  •  执念已碎
    2020-12-08 04:00

    I thought it might be interesting to post some real code for a change. This is the exception class my own utility library uses:

    //---------------------------------------------------------------------------
    // a_except.h
    //
    // alib exception handling stuff
    //
    // Copyright (C) 2008 Neil Butterworth
    //---------------------------------------------------------------------------
    
    #ifndef INC_A_EXCEPT_H
    #define INC_A_EXCEPT_H
    
    #include "a_base.h"
    #include 
    #include 
    
    namespace ALib {
    
    //------------------------------------------------------------------------
    // The only exception thrown directly by alib
    //------------------------------------------------------------------------
    
    class Exception : public std::exception {
    
        public:
    
            Exception( const std::string & msg = "" );
            Exception( const std::string & msg, int line,
                            const std::string & file );
    
            ~Exception() throw();
    
            const char *what() const throw();
            const std::string & Msg() const;
    
            int Line() const;
            const std::string & File() const;
    
        private:
    
            std::string mMsg, mFile;
            int mLine;
    };
    
    //------------------------------------------------------------------------
    // Macro to throw an alib exception with message formatting.
    // Remember macro is not in ALib namespace!
    //------------------------------------------------------------------------
    
    #define ATHROW( msg )                                               \
    {                                                                   \
        std::ostringstream os;                                          \
        os << msg;                                                      \
        throw ALib::Exception( os.str(), __LINE__, __FILE__ );          \
    }                                                                   \
    
    
    }  // namespace
    
    #endif
    

    And this is the .cpp file:

    //---------------------------------------------------------------------------
    // a_except.h
    //
    // alib exception handling stuff
    //
    // Copyright (C) 2008 Neil Butterworth
    //---------------------------------------------------------------------------
    
    #include "a_except.h"
    using std::string;
    
    namespace ALib {
    
    //---------------------------------------------------------------------------
    // exception with optional message, filename & line number
    //------------------------------------------------------------------------
    
    Exception :: Exception( const string & msg ) 
        : mMsg( msg ),  mFile( "" ), mLine(0) {
    }
    
    Exception :: Exception( const string & msg, int line, const string & file ) 
        : mMsg( msg ), mFile( file ), mLine( line ) {
    }
    
    //---------------------------------------------------------------------------
    // Do nothing
    //---------------------------------------------------------------------------
    
    Exception :: ~Exception() throw() {
    }
    
    //------------------------------------------------------------------------
    // message as C string via standard what() function
    //------------------------------------------------------------------------
    
    const char * Exception :: what() const throw() {
        return mMsg.c_str();
    }
    
    //------------------------------------------------------------------------
    // as above, but as C++ string
    //------------------------------------------------------------------------
    
    const string & Exception :: Msg() const {
        return mMsg;
    }
    
    //---------------------------------------------------------------------------
    // File name & line number
    //---------------------------------------------------------------------------
    
    int Exception :: Line() const {
        return mLine;
    }
    
    const string & Exception :: File() const {
        return mFile;
    }
    
    }  // namespace
    
    // end
    

提交回复
热议问题