首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

exception错误处理如何用

2012-04-16 
exception异常处理怎么用#includeiostream#includeexception//应用exceptionusing namespace stddoub

exception异常处理怎么用
#include<iostream>
#include<exception>//应用exception

using namespace std;

double hmean(double a,double b) throw(exception )
{
if(a==-b)
throw exception ();
return 2*a*b/(a+b);
}
int main()
{
double x,y,z;
cout<<"Enter two numbers:";
while (cin>>x>>y)
{
//异常处理
try{
z=hmean(x,y);
cout<<"the result : "<<z<<endl;
}
catch(exception & e)
{
cout<<e.what()<<endl;
cout<<"Enter a new pair of numbers:";
cout<<endl;
continue;//放回循环处,从新输入值
}
}
return 0;
}
================================code分界============================


++++++++++++++++++++++++++++++++问题++++++++++++++++++++++++++++++++

double hmean(double a,double b) throw(exception )
{
if(a==-b)
throw exception ();
return 2*a*b/(a+b);
}
我不明白 double hmean(double a,double b) throw() 这个函数声明 和throw exception()这条语句;
+++++++++++++++++++++++++++++++end++++++++++++++++++++++++++++++++++


[解决办法]
double hmean(double a,double b) throw(exception )
这里是指明这个函数能够抛出哪些类型的异常

throw exception ();
这里是抛出一个exception类型的异常。这里实际上是抛出了该类型的一个临时对象。

热点排行