C++ try catch

头文件:#include <stdexcept>

 1 double divideNumbers(double a,double b)
 2 {
 3     if(b==0)
 4     {
 5         throw std::exception("No zero");
 6     }
 7     return a/b;
 8 }
 9 
10 int main()
11 {
12     try
13     {
14         cout<<divideNumbers(100,5)<<std::endl;
15         cout<<divideNumbers(9,0)<<std::endl;
16     }
17     catch(const std::exception& ex)
18     {
19         cout<<ex.what()<<endl;
20     }
21

 

posted @ 2015-04-07 11:18  扑通  阅读(183)  评论(0编辑  收藏  举报