【发布时间】:2020-06-21 18:19:41
【问题描述】:
我有这个:
// exception::what
#include <iostream> // std::cout
#include <exception> // std::exception
struct ooops : std::exception {
const char* what() const noexcept {return "Ooops!\n";}
};
class A: std::exception{
public:
int tt;
int getTT(int rr){
if (rr ==5) throw ooops();
return rr;
};
};
int main () {
try {
A testA;
int ww = testA.getTT("kkk");
std::cout << ww << std::endl;//throw ooops();
} catch (std::exception& ex) {
std::cout << ex.what();
}
return 0;
}
我想用上面的字符串调用getTT(),我想抛出我的异常消息而不是默认消息。我知道如果我从 Exception 类中重载一个方法,我会没事的,但我想问是否有更简单的方法来做到这一点。我正在阅读有关 Exceptions 的文档,但我找不到有用的东西。
【问题讨论】:
-
“我想用字符串调用
getTT()”getTT()接受一个整数,而不是一个字符串。 “而不是默认的” 你是什么意思?没有“默认例外”之类的东西。您可以抛出任何类型的对象。总而言之,不清楚您要的是什么。 -
老实说,我不明白你打算做什么.. 请解释一下你想要什么
-
为什么
A继承自std::exception?
标签: c++ exception methods overloading