还是附上invaild_argument的简单例子吧....

来源:互联网 发布:空气质量检测软件 编辑:程序博客网 时间:2024/05/27 09:46
// invaild_argument#include <stdexcept>#include <iostream>#define TOSTRING(x) TTOSTRING(x)#define TTOSTRING(x) #xusing namespace std;class ErrMsg{    public:        ErrMsg(const char* _file, const char* _line, const char* _func): file(_file), line(_line), func(_func) {}        ErrMsg() {}        ~ErrMsg() {}        string getFile() const        {            return file;        }        string getLine() const        {            return line;        }        string getFunc() const        {            return func;        }        string getAll() const        {            string ret;            ret += "error:\nFILE: ";            ret += getFile();            ret += "\nLINE: ";            ret += getLine();            ret += "\nFUNC: ";            ret += getFunc();            return ret;        }        private:        const char* file;        const char* line;        const char* func;};class Text{    public:        Text()        {            ErrMsg err(__FILE__, TOSTRING(__LINE__), __FUNCTION__);            throw invalid_argument(err.getAll());        }};int main(){    try    {        // Text p(); 为声明函数        // Text p; 才是无参构造函数, 每次都搞错.....        Text p;    }    catch (invalid_argument & ia)    {        cerr << ia.what() << endl;    }    return 0;}

0 0
原创粉丝点击