第一节 · 匹配想法 · 正则表达式
#include <stdexcept>
double divide(double a, double b)
{
if (b == 0)
{
throw runtime_error("Division by zero!");
}
return a / b;
}
int main()
{
try
{
cout << divide(10, 2) << endl; // 5
cout << divide(10, 0) << endl; // 抛出异常
}
catch (const runtime_error& e)
{
cerr << "Error: " << e.what() << endl;
}
cout << "Program continues..." << endl;
return 0;
}习题
Last updated