<分区>
Throw 在https://en.cppreference.com/w/cpp/language/throw 中定义为C++ 表达式
.从句法上讲,它后面跟着一个异常类名。例如:
int a = 1, b = 0;
if (b==0){
string m ="Divided by zero";
throw MyException(m); //MyException is a class that inherit std::exception class
}
但是,我看到了其他我不太理解的 throw 语法:
void MyFunction(int i) throw(); // how can we have an expression following a function definition?
或者在自定义异常类中,我们还有:
class MyException : public std::exception
{
public:
MyException( const std::string m)
: m_( m )
{}
virtual ~MyException() throw(){}; // what is throw() in this case?
const char* what() const throw() { // what are the parentheses called?
cout<<"MyException in ";
return m_.c_str();
}
private:
std::string m_;
};
因此,我的问题是:
- 是否存在允许表达式后跟函数定义的通用语法规则?
- 为什么表达式 throw 后要有括号?它们在 C++ 中叫什么?