try catch和throw的使用
代码:
#include <iostream>
#include <math.h>
#include <exception>class MyException : public _exception
{
private:/* data */
public:MyException(/* args */);~MyException();
};MyException::MyException(/* args */)
{
}MyException::~MyException()
{
}int main() {double a, b, c;try{std::cin >>a >> b >> c;}catch(char e){std::cerr << e << '\n';}if (b*b - 4*a*c < 0){throw 'N';} else {std::cout << "x1 = " << ((-b + sqrt(b*b - 4*a*c))/(2*a)) <<"x2 = " << ((b + sqrt(b*b - 4*a*c))/(2*a)) <<std::endl;}}
构造函数与try catch
代码&#