【问题标题】:pass class variable to exception in C++将类变量传递给 C++ 中的异常
【发布时间】:2016-12-28 09:45:38
【问题描述】:

这是上周测试中的一个(修改后的)问题。 我得到了一个带有预定义数字的异常类:

class ErrorException  {

    /**
    * Stub class.
    */
private :static long ErrorCode;

public: ErrorException( string str) {
                cout <<str;
        }
};

long ErrorException::ErrorCode = -444;

我想我应该做的是捕获异常,然后将数字作为错误代码返回,但我不知道如何获取数字。我可以让 catch 返回一个字符串,但不是数字字符串:

#include "stdafx.h"
#include <iostream>
#include "ErrorException.h"

#include "errno.h""
#include <string>;
class FillerFunction {


public :

        virtual int getFillerFunction(int x) throw (ErrorException) = 0;

} // this notation means getFillerFunction is always throwing ErrorException?

double calculateNumber(int y){
//.....
try{
     if (y !=0){
      throw(ErrorException(?????))
      }
};

double catchError(){
      catch(ErrorException& x);
};

我最终让它返回字符串“error”,这并不比使用 if 语句更好。我在 c++ 和动态异常中查找了其他 catch-throw 示例,但我找不到一个示例,其中异常抓取了类中定义的变量。如何访问 ErrorCode,保存更改 ErrorException 的返回类型( )?

【问题讨论】:

  • 这是我得到的代码的骨架。我要问的是:1. 如果我不应该传递它,他们为什么要给我一个数字,以及 2. 我将如何传递它?

标签: c++ class exception


【解决方案1】:

虽然这个问题已经得到解答,但我只想补充几点关于 C++11 中正确异常处理的说明:

首先,不应使用throw(ErrorException),因为它已被弃用:http://en.cppreference.com/w/cpp/language/except_spec

此外,通常建议利用 C++ 提供标准异常类这一事实,我个人通常派生自 std::runtime_error

为了真正利用 C++11 中的异常机制,我建议使用 std::nested_exceptionstd::throw_with_nested,如 StackOverflow herehere 中所述。创建适当的异常处理程序将允许您在代码中获取异常的回溯,而无需调试器或繁琐的日志记录。由于您可以使用派生的异常类来执行此操作,因此您可以向此类回溯添加大量信息! 你也可以看看我的MWE on GitHub,回溯看起来像这样:

Library API: Exception caught in function 'api_function'
Backtrace:
~/Git/mwe-cpp-exception/src/detail/Library.cpp:17 : library_function failed
~/Git/mwe-cpp-exception/src/detail/Library.cpp:13 : could not open file "nonexistent.txt"

【讨论】:

    【解决方案2】:

    在你的 throw 中,你正在构造一个异常对象。如果您喜欢传递数字,则必须提供适当的构造函数。

    又快又脏:

    class ErrorException  {
    
    private :
        static long ErrorCode;
    
    public: 
        ErrorException( string str) {
                    cerr <<str<<endl;
            }
        ErrorException( long mynumeric code) {
                    cerr <<"error "<<mynumericcode<<endl;
            }
    };
    

    捕捉应该是这样的:

    double calculateNumber(int y){
        try {
             if (y !=0){
                throw(ErrorException(227));   
             }
        } catch(ErrorException& x) {
             cout << "Error catched"<<endl; 
        }
     }
    

    必须进一步阐述

    在异常构造函数中打印一些东西是不寻常的。您最好在 catch 中填充所需的信息,以便以后可以使用适当的 getter 访问这些信息。然后将在异常处理程序中进行打印。

    如果你有一个静态错误代码,我想在某个地方你有一个函数可以返回最后一个错误代码,无论发生什么。所以也许你会更新这段代码(但你打算如何用现有的字符串替代它?

    它看起来像这样:

    class ErrorException  {    
    private :
        static long LastErrorCode;
        long ErrorCode; 
        string ErrorMessage;     
    
    public: 
        ErrorException(long ec, string str) : ErrorCode(ec), ErrorMessage(str) 
            {
                    LastErrorCode = ec; 
            }
        static long getLastError() const { return LastErrorCode; }  // would never be reset
        long getError() const { return ErrorCode;  }
        string getMessage() const { return ErrorMessage; }
    };
    

    捕捉应该是这样的:

    double calculateNumber(int y){
        try {
             if (y !=0){
                throw(ErrorException(227,"y is not 0"));   
             }
        } catch(ErrorException& x) {
             cerr << "Error "<<x.getError()<<" : "<<x.getMesssage()<<endl; 
        }
        cout << "Last error ever generated, if any: " << ErrorException::getLastErrror()<<endl;
     }
    

    无论如何,我建议您在重新发明轮子之前先看看std::exception

    【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    相关资源
    最近更新 更多