throw0.cpp
来自「压缩包里有教材<<C++模式设计-基于QT4开源跨平台开发框架>」· C++ 代码 · 共 44 行
CPP
44 行
#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 + =
减小字号Ctrl + -
显示快捷键?