📄 throw0.cpp
字号:
#include <QString>#include <qstd.h>#include <math.h>using namespace qstd;//startclass NegArg { public: NegArg(double d) : m_Val(d), m_Msg("Negative value") {} QString getMsg() { return QString("%1: %2").arg(m_Msg).arg(m_Val); } private: double m_Val; QString m_Msg;};double discr(double a, double b, double c) { double d = b * b - 4 * a * c; if (d < 0) throw NegArg(d); return d;}double quadratic_root1(double a, double b, double c) { return (-b + sqrt(discr(a,b,c))/(2 * a));}int main() { try { cout << quadratic_root1(1, 3, 1) << endl; cout << quadratic_root1(1, 1, 1) << endl; } catch(NegArg& narg) { cout << "Attempt to take square root of " << narg.getMsg() << endl; } cout << "Just below the try block." << endl;}/*Out-1.88197Attempt to take square root of Negative value: -3Just below the try block. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -