将类变量传递给c++中的异常
pass class variable to exception in C++
这是上周测试中的一个(修改过的)问题。我得到了一个异常类,其中有一个预定义的数字:
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_exception
和std::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
。
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Pytorch torch.cholesky忽略异常