引发同一类型的多个异常
Throwing multiple exceptions of the same type
所以我有一个程序,有两个例外。在这两种情况下,我都想抛出一个可以在 main 函数中捕获并在错误消息中使用的字符串。但是,据我对他们的了解
try {
...
} catch(string msg) {
cerr << "..." << msg << "..." << endl;
} catch (string msg2) {
cerr << "..." << msg2 << "..." << endl;
}
是不允许的。有什么方法可以做到上述或类似的事情吗?谢谢
我看到两个用例:
1.您需要两种不同类型的错误。
添加派生自std::exception
的异常类
class MyException1 : public std::exception
{
std::string message;
public:
MyException1(std::string const &what_arg) : message(what_arg) {}
MyException1(char const * what_arg) : message(what_arg) {}
const char* what() const { return message.c_str(); }
};
class MyException2 : public std::exception
{
std::string message;
public:
MyException2(std::string const &what_arg) : message(what_arg) {}
MyException2(char const * what_arg) : message(what_arg) {}
const char* what() const { return message.c_str(); }
};
并抓住那些:
try
{
int a = 5;
// do stuff
if (a == 7)
{
throw MyException1("Error 1 occured in because a == 7.");
}
else if (a == 5)
{
throw MyException1("Error 1 occured because a == 5.");
}
// do more stuff
if (a == 22)
{
throw MyException2("Error 2 occured in because a == 22.");
}
else if (a == 575)
{
throw MyException2("Error 2 occured because a == 575.");
}
}
catch (MyException1 &ex)
{
std::cout << "Exception 1: " << ex.what() << "n";
}
catch (MyException2 &ex)
{
std::cout << "Exception 2: " << ex.what() << "n";
}
注意:对于自定义异常来说,这是一个简单但不是最佳设计,因为std::string
可能会引发并且您的程序将被终止。
2.您需要两条不同的错误消息:
从标头中使用适当类型的异常<stdexcept>
:
try
{
int a = 5;
// do stuff
if (a == 7)
{
throw std::runtime_error("Error 1 occured because a == 7.");
}
else if (a == 5)
{
throw std::runtime_error("Error 2 occured because a == 5.");
}
}
catch (const std::exception &ex)
{
std::cout << "Exception: " << ex.what() << "n";
}
注意:如果唯一需要的行为是不同的输出,则可以在没有自己类型的情况下模拟案例 1 的行为:
try
{
int a = 5;
// do stuff
if (a == 7)
{
throw std::runtime_error("Exception 1: Error 1 occured in because a == 7.");
}
else if (a == 5)
{
throw std::runtime_error("Exception 1: Error 1 occured because a == 5.");
}
// do more stuff
if (a == 22)
{
throw std::runtime_error("Exception 2: Error 2 occured in because a == 22.");
}
else if (a == 575)
{
throw std::runtime_error("Exception 2: Error 2 occured because a == 575.");
}
}
catch (const std::exception &ex)
{
std::cout << ex.what() << "n";
}
使用 std::runtime_error 它有一个接受字符串的构造函数。因此,在抛出时传递不同的值。
throw runtime_error( "msg1");
...
throw runtime_error("msg2");
然后当你捕获时,只需在对象中打印消息
...
catch( exception& e ){
cout << e.what() << endl;
}
第一:你的编译器应该为此发出警告,因为第二个catch
永远不会被执行,因为它们具有确切的签名。此外,你不能在第一个异常处于活动状态时抛出第二个异常(在堆栈展开期间抛出的异常尚未进入catch
块),否则,运行时将终止你的程序。
其次:更喜欢通过引用来捕获您的异常
第三:希望你的异常对象落在std::exception
的继承树中。
最后:你到底想做什么?
相关文章:
- 您应该在什么时候创建自己的异常类型
- 为什么标准不允许通过引用捕获不完整的异常类型?
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 提出异常并处理C 的某些异常类型的正确方法是什么?
- 无法在谷歌测试中检查异常类型
- QT创建者和MSVC-忽略调试时特定的异常类型
- 如何捕获除使用 GDB 的异常类型之外的所有异常类型
- 异常是否应具有标志/消息,或者每个错误应具有异常类型
- 如何在不同的上下文(例如线程)中保留原始异常类型信息
- std::rethrow_exception和抛出的异常类型
- 捕获派生异常类型在 Clang/MacOS X 上失败
- C++:检查是否在没有外部库的情况下抛出了某个异常类型
- 如何猜测 libpqxx C++库函数引发的异常类型
- 获取异常类型
- catch语句如何在没有反射的情况下识别异常类型
- 我们应该使用标准库中的异常类型吗?
- 为什么我的程序从异常中崩溃,即使我捕获了异常类型
- 如何在Mac上从c++程序中自动获取异常类型和消息
- close()抛出什么异常类型
- c++中无法捕获异常:无法识别异常类型