c++:捕获运行时错误

发布于 2024-12-05 21:08:00 字数 636 浏览 1 评论 0原文

我正在家里学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声无音无过去 2024-12-12 21:08:00

您需要在 catch 语句旁边添加异常声明。抛出的类型是 std::runtime_error

try
{
  rapidxml::file<char> GpxFile (pcharfilename);
}
catch (const runtime_error& error)
{
   // your error handling code here
}

如果您需要捕获多种不同类型的异常,那么您可以附加多个 catch 语句:

try
{
  rapidxml::file<char> GpxFile (pcharfilename);
}
catch (const runtime_error& error)
{
   // your error handling code here
}
catch (const std::out_of_range& another_error)
{
   // different error handling code
}
catch (...)
{
   // if an exception is thrown that is neither a runtime_error nor
   // an out_of_range, then this block will execute
}

You need to add an exception declaration next to the catch statement. The type thrown is std::runtime_error.

try
{
  rapidxml::file<char> GpxFile (pcharfilename);
}
catch (const runtime_error& error)
{
   // your error handling code here
}

If you need to catch multiple, different kinds of exceptions, then you can tack on more than one catch statement:

try
{
  rapidxml::file<char> GpxFile (pcharfilename);
}
catch (const runtime_error& error)
{
   // your error handling code here
}
catch (const std::out_of_range& another_error)
{
   // different error handling code
}
catch (...)
{
   // if an exception is thrown that is neither a runtime_error nor
   // an out_of_range, then this block will execute
}
瞎闹 2024-12-12 21:08:00
try
{
    throw std::runtime_error("Hi");
}
catch(std::runtime_error& e)
{
   cout << e.what() << "\n";
}
try
{
    throw std::runtime_error("Hi");
}
catch(std::runtime_error& e)
{
   cout << e.what() << "\n";
}
心凉怎暖 2024-12-12 21:08:00

好吧,这取决于当它发生时你想做什么。这是最小值:

try
{
  rapidxml::file<char> GpxFile (pcharfilename);
}
catch (...)
{
   cout << "Got an exception!"
}

如果您想获取实际的异常,那么您需要声明一个变量以将其存储在括号内代替三个点。

Well, it depends on what you want to do when it happens. This is the minimum:

try
{
  rapidxml::file<char> GpxFile (pcharfilename);
}
catch (...)
{
   cout << "Got an exception!"
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文