Qt/C++ Error handling

后端 未结 5 1932
谎友^
谎友^ 2020-12-12 17:14

I\'ve been doing a lot of research about handling errors with Qt/C++ and I\'m still as lost as when I started. Maybe I\'m looking for an easy way out (like other languages p

5条回答
  •  执笔经年
    2020-12-12 17:34

    I prefer error handling using exceptions. Please find the below sample code:

    ErrorStatus ExplodeToLine()
    {
        var errorStatus = new ErrorStatus();
    
        try
        {
            errorStatus = fun();
    
            if (!errorStatus.ok())
            {
                throw new VicException(L"fun failed");
            }
    
    
            errorStatus = fun1();
    
            if (!errorStatus.ok())
            {
                throw new VicException(L"fun1 failed");
            }
    
    
            errorStatus = fun2();
    
            if (!errorStatus.ok())
            {
                throw new VicException(L"fun2 failed");
            }
    
            errorStatus.setError(ErrorType.OK);
        }
        catch (VicException vicExp)
        {
            Log(vicExp.errorMsg());
        }
        catch (Exception exp)
        {
            Log(exp.errorMsg());
        }
    
        return error_status;
    }
    

提交回复
热议问题