c++学习笔记--一个简单的 try-catch-throw

来源:互联网 发布:今年双十一淘宝成交额 编辑:程序博客网 时间:2024/06/16 06:14
#include<iostream>using namespace std;int Div(int x,int y){if(y == 0)throw y;return x/y;}void main(){int x,y,z;cin>>x>>y;try{z = Div(x,y);}catch(int y){cout<<"输入了"<<y<<endl;}catch(...){cout<<"Div error!"<<endl;return;}}
#include <iostream>using namespace std; double division(int a, int b){   if( b == 0 )   {      throw "Division by zero condition!";   }   return (a/b);} int main (){   int x = 50;   int y = 0;   double z = 0;    try {     z = division(x, y);     cout << z << endl;   }catch (const char* msg) {     cerr << msg << endl;   }    return 0;}



原创粉丝点击