abcexceptionchar.cpp

来自「《数据结构、算法与应用》从C++语言应用角度列举了要点」· C++ 代码 · 共 28 行

CPP
28
字号
// function to compute an expression using int value parameters
// throw an exception of type char* in case one of the parameters is <= 0

#include<iostream>

using namespace std;

int abc(int a, int b, int c)
{
   if (a <= 0 || b <= 0 || c <= 0)
          throw "All parameters should be > 0";
   return a + b * c;
}

int main()
{
  try {cout << abc(2,0,4) << endl;}
  catch (char* e)
      {
         cout << "The parameters to abc were 2, 0, and 4" << endl;
         cout << "An exception has been thrown" << endl;
         cout << e << endl;
         return 1;
      }
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?