c ++查找字符串

Program 1:

程序1:

#include <iostream>
using namespace std;
int main()
{
try {
int num1 = 10;
int num2 = 0;
int res = 0;
res = num1 / num2;
}
catch (exception e) {
cout << "Exception: Divide By Zero" << endl;
}
return 0;
}

Output:

输出:

Floating point exception

Explanation:

说明:

Here, we created two code blocks "try" and "catch". We declared 3 local variables in num1, num2, and res.

在这里,我们创建了两个代码块“ try”和“ catch”。 我们在num1num2res中声明了3个局部变量。

Here, variable num1 is initialized with 10 and num2 initialized with 0 then the below expression will generate a runtime exception:

在这里,变量num1初始化为10, num2初始化为0,则下面的表达式将生成运行时异常:

res = num1/num2;

Because the above expression will generate the situation of "Divide By Zero". But here we did not use "throw" to throw the exception, that's why the program will crash at runtime.

因为上面的表达式将产生“除以零”的情况。 但是这里我们没有使用“ throw”引发异常,这就是程序在运行时崩溃的原因。

Program 2:

程式2:

#include <iostream>
#define DEVIDE_BY_ZERO 101
using namespace std;
int main()
{
try {
int num1 = 10;
int num2 = 0;
int res = 0;
if (num2 == 0)
throw DEVIDE_BY_ZERO;
res = num1 / num2;
}
catch (int exp_code) {
cout << "Exception code: " << exp_code << endl;
}
return 0;
}

Output:

输出:

Exception code: 101

Explanation:

说明:

Here, we created two code blocks "try" and "catch". We declared 3 local variables in num1, num2, and res.

在这里,我们创建了两个代码块“ try”和“ catch”。 我们在num1num2res中声明了3个局部变量。

Here, variable num1 is initialized with 10 and num2 initialized with 0 then the below expression will generate a runtime exception:

在这里,变量num1初始化为10, num2初始化为0,则下面的表达式将生成运行时异常:

res = num1/num2;

But here we handled the exception; if num2 is 0 then defined error code will be thrown and caught by "catch" block.

但是我们在这里处理了异常; 如果num2为0,则将抛出定义的错误代码,并由“ catch”块捕获。

In the catch block, we printed the received exception code using cout.

在catch块中,我们使用cout打印收到的异常代码。

Program 3:

程式3:

#include <iostream>
using namespace std;
int main()
{
try {
int num1 = 10;
int num2 = 0;
int res = 0;
if (num2 == 0)
throw "Divide By Zero";
res = num1 / num2;
}
catch (char* exp) {
cout << "Exception: " << exp << endl;
}
return 0;
}

Output:

输出:

terminate called after throwing an instance of 'char const*'
Aborted (core dumped)

Explanation:

说明:

The above crashed at runtime because here we have thrown a constant string ("Divide By Zero") using "throw" keyword, and using char *exp in the "catch" block.

上面的代码在运行时崩溃,因为在这里我们使用“ throw”关键字并在“ catch”块中使用char * exp抛出了一个常量字符串(“ Divide By Zero”)。

To resolve the problem we need to use const char *exp instead of char *exp.

要解决该问题,我们需要使用const char * exp而不是char * exp

Program 4:

计划4:

#include <iostream>
using namespace std;
void funDiv(int X, int Y)
{
int res = 0;
if (Y == 0)
throw "Divide By Zero";
res = X / Y;
cout << res << endl;
}
int main()
{
try {
int num1 = 10;
int num2 = 0;
funDiv(num1, num2);
}
catch (const char* exp) {
cout << "Exception: " << exp << endl;
}
return 0;
}

Output:

输出:

Exception: Divide By Zero

Explanation:

说明:

Here, we defined a function funDiv() with two arguments X and Y. Here, we checked if the value of Y is 0 then it will throw a string message using the "throw" keyword.

在这里,我们定义了一个带有两个参数XY的 funDiv()函数。 在这里,我们检查Y的值是否为0,然后它将使用“ throw”关键字抛出字符串消息。

Now coming to the main() function, here we declared local variable num1 and num2 inside the “try” block, and call funDiv() function to perform division.

现在进入main()函数,在这里我们在“ try”块中声明了局部变量num1num2 ,并调用funDiv()函数执行除法。

The string message is thrown by funDiv() function, caught by the "catch" block, and print the message using cout on the console screen.

字符串消息由funDiv()函数引发,并由“ catch”块捕获,并在控制台屏幕上使用cout打印该消息。

Program 5:

计划5:

#include <iostream>
#include <exception>
using namespace std;
void funDiv(int X, int Y)
{
int res = 0;
if (Y == 0) {
exception E;
throw E;
}
res = X / Y;
cout << res << endl;
}
int main()
{
try {
int num1 = 10;
int num2 = 0;
funDiv(num1, num2);
}
catch (exception) {
cout << "Exception generated";
}
return 0;
}

Output:

输出:

Exception generated

Explanation:

说明:

Here, we defined a function funDiv() with two arguments X and Y. Here, we checked if the value of Y is 0 then it will throw an object of exception class using the "throw" keyword.

在这里,我们定义了一个带有两个参数XY的 funDiv()函数。 在这里,我们检查Y的值是否为0,然后它将使用“ throw”关键字抛出异常类的对象。

Now coming to the main() function, here we declared local variable num1 and num2 inside the “try” block, and call funDiv() function to perform division.

现在进入main()函数,在这里我们在“ try”块中声明了局部变量num1num2 ,并调用funDiv()函数执行除法。

The exception object was thrown by funDiv() function, caught by the "catch" block, and print the message using cout on the console screen.

异常对象由funDiv()函数抛出,并由“ catch”块捕获,并在控制台屏幕上使用cout打印消息。

翻译自: https://www.includehelp.com/cpp-tutorial/exceptional-handling-find-output-programs-set-1.aspx

c ++查找字符串

c ++查找字符串_C ++异常处理| 查找输出程序| 套装1相关推荐

  1. c ++查找字符串_C ++数组| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { char* STR[] = ...

  2. c ++查找字符串_C ++结构| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { typedef struct ...

  3. c ++查找字符串_C ++结构| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; str ...

  4. c ++查找字符串_C ++结构| 查找输出程序| 套装3

    c ++查找字符串 Program 1: 程序1: #include <iostream> #pragma pack(1) using namespace std; typedef str ...

  5. 查找字符串中要查找的字符串最后一次出现的位置

    C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> #i ...

  6. c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  7. c ++查找字符串_C ++类和对象| 查找输出程序| 套装3

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; publi ...

  8. c ++查找字符串_C ++类和对象| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  9. c ++查找字符串_C ++类和对象| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private int ...

最新文章

  1. Python PK C++,究竟谁更胜一筹?
  2. 网络流学习(转载自ssw 的博客)
  3. 第四周项目三-随机数函数应用于游戏(猜数字游戏)
  4. hankel--生成Hankel矩阵
  5. 审查指南 最新版本_代码审查-最终指南
  6. es6去除重复项_Excel的去除重复项你真的明白原理吗?浅尝去除重复项的函数奥秘...
  7. qq浏览器网页版_QQ邮箱回应部分用户登录异常:系后台服务波动,问题已解决...
  8. linux 常用SHELL
  9. python弹球游戏移动球拍_python pygame实现挡板弹球游戏的代码
  10. Atitit.eclise的ide特性-------abt 编译
  11. 【UVA140】Bandwidth(最优性剪枝+全排列+思路)
  12. Camera(2) camera i2c总线协议介绍
  13. 《孽海记·思凡》唱段·风吹荷叶煞
  14. 试用期没过,因在公司上了 1024 网站...
  15. 数学之美-读书笔记11-15章
  16. 微信企业付款到银行卡(微信转账)(Java完整版)
  17. 虚拟机安装安装增强失败:modprobe vboxguest failed
  18. 正切tan的概念实体化
  19. php格式转换成docx,如何在PHP中修改.doc或.docx文件
  20. Echarts柱状图实现点击事件

热门文章

  1. 动态新增表字段_制作动态的数据透视表(一):定义名称法创建数据透视表
  2. .net 从txt中读取行数据_【VBA项目】从指定文件中读取数据并绘制图表
  3. 定时器和promise_从Promise链理解EventLoop
  4. pos共识机制_OK区块链60讲 | 第17集:什么是PoS共识机制
  5. Linux的启动流程简析(以Debian为例)
  6. 十二赞日志收集与报警系统一览
  7. 华大基因茅矛:云计算让精准医疗走进生活
  8. Spring Data Redis实战之提供RedisTemplate
  9. 一步一步搭建客服系统 (7) 多人共享的电子白板、画板
  10. github果然强大