重点了解,捕获异常时对异常信息的详细处理
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double triangle(double a, double b, double c)
{
double s = (a+b+c)/2;
if(a+b<=c || a+c<=b || b+c<=a)
{
throw a;
}
double e = sqrt(s*(s-a)*(s-b)*(s-c));
return e;
}
int main()
{
double a, b, c;
cout << "input a, b, c: ";
cin >> a >> b >> c;
while(a>0 && b>0 && c>0)
{
try{
double d = triangle(a, b, c);
cout << "d: " << d << endl;
}
catch(int e){
cout << "the exception happen's : " << e << endl;
cout << "a= " << a << ",b= " << b << ",c= " << c
<< "this not a triangle" << endl;
exit(0);
}
catch(exception e){
cout << "catch exception e" << endl;
cout << e.what() << endl;
exit(0);
}
catch(...){
cout << "catch exception that unknown.." << endl;
exit(0);
}
cout << "input a, b, c" ;
cin >> a >> b >> c;
}
return 0;
}