Throw is defined as a C++ expression
in https://en.cppreference.com/w/cpp/language/throw. Syntactically, it is followed by an exception class name. For example:
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
}
However, I have seen other syntaxes with throw that I don't quite understand:
void MyFunction(int i) throw(); // how can we have an expression following a function definition?
or within a custom exception class, we also have:
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_;
};
Therefore, my questions are:
- Is there a common syntax rule that allows an expression followed by a function definition?
- Why do we have parenthesis following an expression throw? What are they called in C++?