为什么我不能从抛出异常中打印出错误? [英] How come I don't can't print out error from my throw exception?
问题描述
我只是在学习引发异常,我写了一个代码,但似乎无法打印出 Error:太大
或 Error :当我输入
。我如何解决它,以便它可以打印出 10000
或 100000
时太大 Error:太大
或 Error:toolarge
。
I am just learning throw exception and I wrote a code but it doesn't seem like I can print out Error: too big
or Error:too large
when I entered 10000
or 100000
. How can I fix it so that it can print out Error: too big
or Error:too large
.
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
int xxx;
cout<<"please enter xxx:";
cin>>xxx;
if (xxx==1)
cout<<"2";
else if (xxx==10)
cout<<"3";
else if (xxx==100)
cout<<"4";
else if (xxx==1000)
cout<<"5";
else if (xxx==10000)
throw "too big";
else if (xxx==100000)
throw "too large";
return 0;
}
推荐答案
您似乎归因于太多
但是从本质上讲,异常非常简单。基本上,这只是意味着一些低级代码创建了一个对象(通过 throw
),该对象最终以一些高级代码(通过 catch
)。
But at the core of it, exceptions are really simple. Basically, it just means that some low-level code creates an object (via throw
) that ends up in some higher-level code (via catch
).
当没有渔获量
时,异常会不断下降函数调用堆栈,并一直这样做,直到它掉下 main
。
When there is no catch
, then the exception keeps "falling down" the function-call stack, and it keeps doing so until it "falls off" main
.
,发生了一系列特殊事件。您可以使用相对高级的技术来配置行为,但是初学者真正需要知道的是,在这种情况下,您不能保证会打印任何错误消息。如果要使异常导致错误消息,则必须自己编写必要的代码。
When this happens, a series of special events takes place. You can configure the behaviour with relatively advanced techniques, but all a beginner really needs to know is that you are not guaranteed any error message being printed in this case. If you want an exception to result in an error message, then you have to write the necessary code yourself.
适应原始示例,该代码可能如下所示:
Adapting your original example, that code may look like this:
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
try
{
int xxx;
cout<<"please enter xxx:";
cin>>xxx;
if (xxx==1)
cout<<"2";
else if (xxx==10)
cout<<"3";
else if (xxx==100)
cout<<"4";
else if (xxx==1000)
cout<<"5";
else if (xxx==10000)
throw "too big";
else if (xxx==100000)
throw "too large";
return 0;
}
catch (char const* exc)
{
cerr << exc << "\n";
}
}
但是,这里增加了混乱的是除其他大多数语言外,C ++允许您抛出任何类型的对象。
However, what adds to the confusion here is that unlike most other languages with exceptions, C++ allows you to throw objects any type.
让我们在这里仔细看一下这一行:
Let's take a closer look at this line here:
throw "too big";
太大
是类型为 char const [8]
的文字。 C ++允许您抛出这样的对象。而且由于可以将其转换为 char const *
的第一个元素,因此您甚至可以捕获它和其他大小的字符串文字作为 char const *
。
"too big"
is a literal of type char const[8]
. C++ allows you to throw such an object. And since it can be converted to a char const*
to its first element, you can even catch it and string literals of other sizes as char const*
.
但这不是惯常做法。相反,您应该抛出直接或间接从 std :: exception
派生的对象。
But that's not common practice. You should instead throw objects derived directly or indirectly from std::exception
.
以下是使用标准的示例类 std :: runtime_error
从< stdexcept>
标头中。
Here is an example using the standard class std::runtime_error
from the <stdexcept>
header.
(您的原始代码未使用您可以完全使用 throw
本身,而无需< stdexcept>来使用
。) < stdexcept>
中的任何内容。
(Your original code did not use anything from <stdexcept>
. You can perfectly use throw
itself without <stdexcept>
.)
#include <iostream>
#include <stdexcept>
#include <exception>
int main()
{
try
{
int xxx;
std::cout << "please enter xxx:";
std::cin >> xxx;
if (xxx==1)
std::cout << "2";
else if (xxx==10)
std::cout << "3";
else if (xxx==100)
std::cout << "4";
else if (xxx==1000)
std::cout << "5";
else if (xxx==10000)
throw std::runtime_error("too big");
else if (xxx == 100000)
throw std::runtime_error("too large");
return 0;
}
catch (std::exception const& exc)
{
std::cerr << exc.what() << "\n";
}
}
这篇关于为什么我不能从抛出异常中打印出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!