C++捕捉异常

#include iostreamusing namespace std;double division(int a, int b) {if( b == 0 ) {throw Division by zero condition!;

编程学习网为您整理以下代码实例,主要实现:C++捕捉异常,希望可以帮到各位朋友。

#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;
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐