1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <iostream>
using std::cin; using std::cout; using std::endl;
double divide(int n1, int n2);
int main() { try { double n3 = divide(1, 0); cout << "相除得:" << n3 << endl; } catch (std::runtime_error err) { cout << "相除异常:" << err.what() << endl; } }
double divide(int n1, int n2) { if (n2 == 0) throw std::runtime_error("被除数不能为0!"); return (double)n1 / n2; }
|