【发布时间】: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. 我将如何传递它?