c++:捕获运行时错误
我正在家里学习 C++,并且正在使用rapidxml 库。 我正在使用它提供的实用程序来打开文件:
rapidxml::file<char> myfile (&filechars[0]);
我注意到如果 filechars
错误,rapidxml::file
会抛出一个runtime_error:
// Open stream
basic_ifstream<Ch> stream(filename, ios::binary);
if (!stream)
throw runtime_error(string("cannot open file ") + filename);
stream.unsetf(ios::skipws);
我想我需要写一些类似的东西那:
try
{
rapidxml::file<char> GpxFile (pcharfilename);
}
catch ???
{
???
}
我做了一些谷歌搜索,但我没有在 ???
的位置找到我需要的东西。
有人可以帮助我吗? 谢谢!
I am learning c++ at home and I am using the rapidxml lib.
I am using the utils provided with it to open files:
rapidxml::file<char> myfile (&filechars[0]);
I noticed that if filechars
is wrong the rapidxml::file
throw a runtime_error:
// Open stream
basic_ifstream<Ch> stream(filename, ios::binary);
if (!stream)
throw runtime_error(string("cannot open file ") + filename);
stream.unsetf(ios::skipws);
I think I need to write something like that:
try
{
rapidxml::file<char> GpxFile (pcharfilename);
}
catch ???
{
???
}
I made some googling, but I did not find what I need in the place of the ???
.
Could somebody help me?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在
catch
语句旁边添加异常声明。抛出的类型是 std::runtime_error。如果您需要捕获多种不同类型的异常,那么您可以附加多个
catch
语句:You need to add an exception declaration next to the
catch
statement. The type thrown is std::runtime_error.If you need to catch multiple, different kinds of exceptions, then you can tack on more than one
catch
statement:好吧,这取决于当它发生时你想做什么。这是最小值:
如果您想获取实际的异常,那么您需要声明一个变量以将其存储在括号内代替三个点。
Well, it depends on what you want to do when it happens. This is the minimum:
If you want to get at the actual exception, then you need to declare a variable to store it in inside the parentheses in place of the three dots.