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;
}

本文标题为:C++除零异常

上一篇: C++读写示例
下一篇: C++定义新异常

基础教程推荐