c++: Catch runtime_error
c++: Catch runtime_error
我正在家里学习c++,我正在使用rapidxml库。我正在使用它提供的utils来打开文件:
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 ???
{
???
}
我做了一些谷歌搜索,但我没有找到我需要的地方的???
。
有人能帮我吗?谢谢!
需要在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
}
try
{
throw std::runtime_error("Hi");
}
catch(std::runtime_error& e)
{
cout << e.what() << "n";
}
嗯,这取决于当它发生时你想做什么。这是最小值:
try
{
rapidxml::file<char> GpxFile (pcharfilename);
}
catch (...)
{
cout << "Got an exception!"
}
如果您想获得实际的异常,那么您需要声明一个变量,将其存储在圆括号内,以代替三个点。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- "runtime error: addition of unsigned offset to 0x129000a0 overflowed to 0x12900088"
- 为什么我的代码中出现'Runtime Error - SIGSEGV'?
- C++ dynamic_cast runtime-error
- 错误"Runtime error"
- 如何修复不影响性能的"runtime error"?
- 谁能告诉我为什么它显示"runtime error"?
- Lua+Luabind,"runtime error"后堆栈顶部没有错误信息
- 如何解决C代码中"runtime error exitcode 6"报告的错误?
- 在范围结束后提升共享指针"runtime error"
- C++ 'Runtime error time: 0 memory: 3452 signal:11'