将类变量传递给c++中的异常

pass class variable to exception in C++

本文关键字:异常 c++ 类变量      更新时间:2023-10-16

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

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()的返回类型?

虽然这个问题已经有了答案,但我只想在c++ 11中添加一些关于正确处理异常的注释:

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

此外,通常建议利用c++提供标准异常类的事实,我个人通常从std::runtime_error派生。

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

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"

在throw中,你正在构造一个异常对象。如果您希望传递一个数字,则必须提供相应的构造函数。

Quick and dirty:

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