zoukankan      html  css  js  c++  java
  • [原创]异常处理(修改)

    异常处理

    自己做的异常处理类,还没有继承结构的,目前正用在一个项目上。虽然还不够完善,也没能是程序真正终止,但感觉还不错。以后有机会再次完善。

     

    一、错误处理

    程序上的错误,我把它分为两种,一种是给程序员看的,称为内部错误;一种是给用户看的,称为外部错误。

    外部错误

    1、通常由用户的错误操作引起,并提示给用户怎么样操作才正确;
    2、由通用的内部错误引起,提示给用户“出现内部错误”,并把内部错误的信息显示给用户;
    3、由特定的内部错误引起,但由于特定内部错误涉及程序算法而不能公开,所以并不把内部错误的信息提示给用户,只能给用户错误代码,并提示用户联系开发商。

    内部错误

    1、通用内部错误,抛出时需要提供错误原因、错误所在类、错误所在方法
    2、特定内部错误,需要在通用内部错误中再给出错误代码


    通常,如果是DEBUG状态,则直接显示第一种内部错误,便于程序员查看和修改代码;如果是RELEASE状态,则把内部错误转换成能使用户理解的外部错误,便于用户进一步做相关操作。

    二、错误代码

    错误样式:0xe0030201
    错误用e表示,通用错误(比如除数为0,参数为空等等)用c表示
    0表示内部错误,1表示外部错误
    03类代码
    02方法代码
    01错误代码

    这样的错误代码体系,支持255个类,每个类支持255个方法,每个方法支持255种错误

    三、构造

    当错误发生的时候,利用下面的参数构造异常类

    错误标志('e'或'c'),错误类型(内部还是外部,即0或1),类名,方法名,错误代码,错误描述,附加信息(主要说明错误产生原因),显示方式

    类名和方法名是字符串,如果是DEBUG版本的,则可以直接显示出来,如果是RELEASE版本的,则需要从该字符串转换为相应的代码

    显示方式表示是在控制台显示,还是显示一个MessageBox

    错误代码虽然是自己定义的,为了便于处理,这里把类、方法和错误的代码都用字符串表示,便于拼接

    四、异常类

    根据上面的分析,提出一种错误处理的类,称为异常类。

    在工程的每个类里建立两个静态的字段:

    static string className="name of this class" const;
    static string classCode="01" const;

     

    classCode中的数值给每个类一个不重复的十六进制的编号(两位)。

    然后再在类里的每个方法中,建立两个变量:

    string methodName="name of this method";
    string methodCode="01";

     

    methodCode给同一个类中的每个方法一个不重复的十六进制的编号(也是两位)。

    在每个方法里,按照可能出现错误的顺序,给每个错误一个不重复的十六进制编号(也是两位)。


    在CMyException的构造函数中,在className参数后面添加classCode参数,在methodName参数后面添加methodCode参数。这样就不需要在CMyException中使用map类记录Name和Code了。但是如果工程里面类非常多,或者某些类里的方法非常多,那么为了将来根据错误码查阅上的方便,可能需要建立一个ErrCode.h头文件,记录每个类和方法非常多的类中的每个方法所对应的Code(这个文件需要针对每个工程自行编辑)。


    五、设计

    目前还不是很完善。修改掉了代码的错误,使代码能够正常运行(原来那个就算是伪代码吧。。。)

    .h文件中的代码:

    View Code

    class CMyException
    {
    public:
        CMyException(
    string eFlag,string eType,string className,string classCode,string methodName,string methodCode,string errCode,string description,string extraInfo,int showType);
        
    virtual ~CMyException();

        
    // 定义出下面的属性后,当底层抛出这个异常后,顶层可以根据实际情况的需要(DEBUG还是RELEASE等),显示其中必要的信息
        string m_eFlag;
        
    string m_eType;
        
    string m_className;
        
    string m_classCode;
        
    string m_methodName;
        
    string m_methodCode;
        
    string m_errCode;
        
    string m_description;
        
    string m_extraInfo;
        
    int m_showType;

        
    virtual void showMessage();
        
    virtual void showDebugMessage();
        
    virtual void showReleaseMessage();
        
    virtual void showReleaseMessage(string msgDescription);
        
    private:
    //     map<string,string> m_classMap;
    //     map<string,string> m_methodMap;
        
        
    void showMessage(string msg,int showType);
        
    string getErrorCode(string className,string methodName);
    };

    .cpp中的代码:

    View Code

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    CMyException::CMyException(
    string eFlag,string eType,string className,string classCode,string methodName,string methodCode,string errCode,string description,string extraInfo,int showType)
    :m_eFlag(eFlag),m_eType(eType),m_className(className),m_classCode(classCode),
     m_methodName(methodName),m_methodCode(methodCode),m_errCode(errCode),
     m_description(description),m_extraInfo(extraInfo),m_showType(showType)
    {

    }

    CMyException::
    ~CMyException()
    {

    }


    void CMyException::showMessage()
    {
    #ifdef _DEBUG
        showDebugMessage();
    #else
        
    if(m_eType.compare("0")==0)
            showReleaseMessage();
    #endif
    }

    void CMyException::showDebugMessage()
    {
        
    string msgHead="程序运行过程中产生了如下的异常";
        
    string msgReason="这个异常可能由以下原因引起";
        
    string msgTrace="这个异常产生于 " + m_className + " 类的 " + m_methodName + " 方法中。";
        
    string msg=msgHead + "\n\n" + m_description + "\n\n" + msgReason + "\n\n" + m_extraInfo + "\n\n" + msgTrace;
        
        showMessage(msg,m_showType);
    }

    void CMyException::showReleaseMessage()
    {
        
    string msgHead="程序运行过程中产生了如下的异常";
        
    string msgErrorCode=getErrorCode(m_className,m_methodName);
        
    string msg=msgHead + "\n\n" + m_description + "\n\n" + "(错误码:" + msgErrorCode + ")";
        
        showMessage(msg,m_showType);
    }

    void CMyException::showReleaseMessage(string msgDescription)
    {
        
    string msgErrorCode=getErrorCode(m_className,m_methodName);
        
    string msg=m_description + "\n\n" + "(错误码:" + msgErrorCode + ")";
        
        showMessage(msg,m_showType);
    }

    void CMyException::showMessage(string msg,int showType)
    {
        
    switch(showType)
        {
        
    case 0:   // Console
            cout<<msg;
            
    break;
        
    case 1:   // Window
            AfxMessageBox(msg.c_str());
            break;
        }
    }

    string CMyException::getErrorCode(string className,string methodName)
    {
        
    //    string classCode="",methodCode="";
        
        
    //    map<string, string>::iterator iter;
        
    //    classCode=m_classMap.find(className);
        
    //    if(iter != m_classMap.end())
        
    //        classCode=iter->second;
        
    //    else
        
    //        classCode="FF";
        
        
    //    methodCode=m_methodMap.find(methodName);
        
    //    if(iter != m_methodMap.end())
        
    //        methodCode=iter->second;
        
    //    else
        
    //        methodCode="FF";
        
        
    //    string errorCode=m_eFlag+m_eType+classCode+methodCode+m_errCode;
        
        
    string errorCode="0x"+m_eFlag+m_eType+m_classCode+m_methodCode+m_errCode;
        
        
    return errorCode;
    }

    运行效果图:

    DEBUG

    RELEASE


    作者:海阔天

    欢迎转载!转载请注明出处:

    www.cnblogs.com/yxsylyh

  • 相关阅读:
    Spring-AOP切面编程(3)
    【SpringBoot】SpingBoot整合AOP
    反射--Reflection
    泛型--Generic
    C#系统库的源代码
    C#中的?
    C#语法糖
    C#初识LINQ
    C#委托和事件的区别
    C#中的lambda表达式
  • 原文地址:https://www.cnblogs.com/yxsylyh/p/exception.html
Copyright © 2011-2022 走看看