一个关于C++异常的疑惑!!!~~~
下面代码:
- C/C++ code
#include "stdafx.h"#include "iostream"#include "exception"using std::exception;using std::cin;using std::cout;using std::endl;int hmean(int a,int b);int _tmain(int argc, _TCHAR* argv[]){ int x,y,z; cout<<"Enter two numbers :"; while(cin>>x>>y&& x!=0&&y!=0) { try { z = hmean(x,y); } catch(exception e) { cout<<e.what()<<endl; cout<<"Enter a new pair of numbes: "; continue; } std::cout<<"Harmonic nean of "<<x<<" and "<<y <<" is "<<z<<endl; cout<<"Entre next set of numbers <0 0to quit>: "; } return 0;}int hmean(int a,int b) throw (exception ){ cout<<" 抓不住!~~~~"<<endl; return 2*a*b/(a+b); //分母为零时会抛异常}
想问 是不是在C++中不将异常throw 就不会抓住呢?
因为在这个程序中是抓不住的!~
[解决办法]
谁说C++中1/0时会抛异常?