[英]Throwing an exception
I have created one exception in a function like so: 我在这样的函数中创建了一个异常:
void testing(int &X)
{
....
X=...
if (X>5)
throw "X greater than 5!"
}
and then in main.cpp 然后在main.cpp中
try
{
int X=0;
testing(X);
}
catch (const char *msgX)
{
....
}
But now I would like to also introduce Y as X. and the prototype of testing will be : 但是现在我也想将Y引入X。测试的原型将是:
void testing(int &X, int &Y)
My question, how can I throw two exceptions, in which if X>5, I throw an exception regarding X and if Y>10, I throw another exception about Y and I catch them all at the end in my main program? 我的问题是,如何抛出两个异常,如果X> 5,则引发有关X的异常,如果Y> 10,则引发有关Y的另一个异常,并在主程序中将它们全部捕获?
In C++ it is impossible to have two exceptions "in flight" at the same time. 在C ++中,不可能同时“运行”两个异常。 If that condition ever arises (eg by a destructor throwing during stack unwinding), the program is terminated (with no way to catch the second exception). 如果出现这种情况(例如,在堆栈展开时析构函数抛出),则程序将终止(无法捕获第二个异常)。
What you can do is make a suitable exception class, and throw that. 您可以做的是创建一个合适的异常类,然后将其抛出。 For example: 例如:
class my_exception : public std::exception {
public:
my_exception() : x(0), y(0) {} // assumes 0 is never a bad value
void set_bad_x(int value) { x = value; }
void set_bad_y(int value) { y = value; }
virtual const char* what() {
text.clear();
text << "error:";
if (x)
text << " bad x=" << x;
if (y)
text << " bad y=" << y;
return text.str().c_str();
}
private:
int x;
int y;
std::ostringstream text; // ok, this is not nothrow, sue me
};
Then: 然后:
void testing(int &X, int &Y)
{
// ....
if (X>5 || Y>10) {
my_exception ex;
if (X>5)
ex.set_bad_x(X);
if (Y>10)
ex.set_bad_y(Y);
throw ex;
}
}
Anyway you should never throw raw strings or integers or the like--only classes derived from std::exception (or perhaps your favorite library's exception classes, which hopefully then derive from there, but may not). 无论如何,您绝不应该抛出原始字符串或整数之类的东西-只能从std :: exception派生的类(或者也许是您最喜欢的库的异常类,希望可以从那里派生,但可能不是)。
You can throw different exception types or you can through the same exception type with different content. 您可以引发不同的异常类型,也可以通过相同的异常类型使用不同的内容。
struct myexception : public std::exception
{
std::string description;
myexception(std::string const & ss) : description(ss) {}
~myexception() throw () {} // Updated
const char* what() const throw() { return description.c_str(); }
};
void testing(int &X, int &Y)
{
if (X>5)
throw myexception("X greater than 5!")
if (Y>5)
throw myexception("Y greater than 5!")
}
try
{
int X=0;
testing(X);
}
catch (myexception const & ex)
{
}
(btw. I did not down-vote...) (顺便说一句,我没有拒绝投票...)
Here is a sketch: 这是一个草图:
class x_out_of_range : public std::exception {
virtual const char* what() { return "x > 5"; }
};
class y_out_of_range : public std::exception {
virtual const char* what() { return "y > 10"; }
};
Now in your function: 现在在您的函数中:
if (x > 5)
throw x_out_of_range();
:
if (y > 10)
throw y_out_of_range();
Now your catch code: 现在,您的捕获代码:
try
{
:
}
catch (x_out_of_range const& e)
{
}
catch (y_out_of_range const& e)
{
}
NOTE: in any case, you can only throw one exception from the function... 注意:无论如何,您只能从函数中引发一个异常...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.