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

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 ++查找字符串

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542372.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python的repr和str有什么不同_str()和repr()的异同

str()函数和repr()函数&#xff0c;都是Python内置的标准函数。这两个函数都是根据参数对象返回一个字符串&#xff0c;但是又有一些不一样的地方。我们在使用的时候&#xff0c;常常搞混&#xff0c;倾向于使用简单明了的str()函数&#xff0c;而搞不清楚为什么还有一个不知所…

android web通讯录,Android手机开发之通讯录

Android手机开发——通讯录实现增加、查询、修改、删除的功能&#xff0c;输入联系人信息&#xff0c;点击“添加”按钮&#xff0c;可以添加联系人信息到数据库&#xff1b;点击“查询”按钮&#xff0c;会发现添加的联系人信息显示在界面中&#xff1b;重新输入联系人电话&am…

有关UITableView--cell复用问题

近来用Tableview做了一个九宫格。过程中碰到了两个cell复用问题。 问题一&#xff1a; 在cell中为button添加addTarget点击事件时&#xff0c;出现后面的cell会重叠它前面cell的事件。代码如下&#xff1a; C代码 static NSString *CellWithIdentifier "DiscoverHomeTab…

python客户端和服务端实验_结合服务器和客户端python

我正在尝试使用python(稍后可能用c语言)和TCP套接字制作一个本地网络聊天程序。我的目的是让服务器监听当前计算机的地址以获取传入消息&#xff0c;并将这些消息转发给客户端(我现在还不确定)。客户端将是一个简单的gui&#xff0c;可以通过本地连接向活动服务器发送消息。实际…

python常用语法和示例_C语言切换案例教程,语法,示例和规则

python常用语法和示例使用默认情况下的决策 (Decision making using switch-case-default) Many times in our daily lives, we face conditions where we are required to choose between a number of alternatives rather than just two or three. For example, which school…

android so abi适配,Android NDK学习(六): so文件兼容之abiFilters的使用

最近项目中遇到了要使用JavaCV的情况&#xff0c;涉及到了abi兼容的选择。因为如果全部都适配的话&#xff0c;包很大&#xff0c;这样兼容那些用户数极少的cpu就很不划算&#xff0c;所以我只适配了armeabi-v7a这一个。但是今天在x64-v8a的模拟器上看的时候&#xff0c;提示我…

python中doc=parased.getroot()_python中执行sed命令操作源文件时出现错误

我想在python中执行一个sed命令&#xff0c;第一种方法直接指定文件时&#xff0c;可以正确输出结果&#xff0c;但是第二种我打开文件操作的时候就有问题&#xff0c;不知道什么原因&#xff0c;求高手解答&#xff1f;(1)>>> sedcmd"sed -n \s/{//g; p\ /qye/p…

JavaScript基础之Number对象和Math对象

2019独角兽企业重金招聘Python工程师标准>>> //Math对象//属性float Math.E; //返回自然对数的底数e&#xff0c;约2.718float Math.LN2; //返回2的自然对数&#xff0c;约0.693float Math.LN10; //返回10的自然对数&#xff0c;约2.302fl…

c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?

c stl 获取最小值Given a vector and we have to minimum/smallest element using C STL program. 给定一个向量&#xff0c;我们必须使用C STL程序最小/最小元素。 寻找向量的最小元素 (Finding smallest element of a vector) To find a smallest or minimum element of a …

android studio panic,Android Studio模拟器PANIC错误

Android Studio模拟器突然停止工作.当我尝试运行虚拟设备时,我在事件日志中收到以下错误.模拟器:PANIC:找不到AVD系统路径.请定义ANDROID_SDK_ROOT仿真器:处理完成,退出代码为1所以我检查了ANDROID_SDK_ROOT环境变量设置的值,它是空的.所以我把它设置为/Users/{username}/Libra…

linux特殊权限之访问权限

特殊权限如/etc/passwd:sSuid:普通用户以管理员身份运行命令&#xff08;chmod us FILE、chmod u-s FILE&#xff09;如果FILE本身原来就有执行权限&#xff0c;SUID显示为s&#xff1b;否则显示SSgid:基本组以管理组身份运行命令&#xff08;chmod gs FILE、chmod g-s FILE&am…

vb.net变量值变化触发事件_Angular变化检测的理解

获取脏检查的时机Angular 使用NgZone获取变化的通知&#xff0c;然后进行全面的变化检测&#xff0c;进而更新Dom脏检查的过程Angular的数据流是自顶而下&#xff0c;从父组件到子组件单项流动&#xff0c;单项数据流保证了高效可预测的变化检测。尽管检查了父组件之后&#xf…

python 算术右移_Python算术序列| 竞争编码问题

python 算术右移Question: 题&#xff1a; In mathematics, when in an arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant then it is called arithmetic constant. 在数学中&#xff0c;当在算术序列中是…

Android8内测申请,小米 6 安卓 8.0 来了 内测开始招募

Android 8.0 已经正式发布多时&#xff0c;目前不少厂商已经启动了旗下进行的 Android 8.0 适配计划。但令人纳闷的是&#xff0c;一向对系统升级比较热心的小米却迟迟没有动静。好消息是&#xff0c;此前网友曝光的消息显示&#xff0c;MIUI 已经悄然在官方论坛中招募小米 6 的…

My linux

为什么80%的码农都做不了架构师&#xff1f;>>> 1.linux 命令方式修改机器名称 # hostname newHostName # vi /etc/sysconfig/network 修改或增加配置&#xff1a;hostnamenewHostName # vi /etc/hosts 修改对应的本地HOST映射 xx.xxx.xxx.xxx newHostName 2.Redha…

狂神说es笔记_人教版七上英语Unit5电子课本音频+课堂笔记+课后同步习题

1人教 七上英语Unit5单词七年级英语上册Unit 5单词默写1做&#xff1b;干(助动词)__________2做&#xff0c;干(助动词第三人称单数形式)__________3有__________4网球__________5球__________6乒乓球______7球棒&#xff1b;球拍__________8(英式)足球____________________9排…

Java RandomAccessFile getFilePointer()方法与示例

RandomAccessFile类getFilePointer()方法 (RandomAccessFile Class getFilePointer() method) getFilePointer() method is available in java.io package. getFilePointer()方法在java.io包中可用。 getFilePointer() method is used to get the current pointer in the Rando…

先进技术android,React Native实战(JavaScript开发iOS和Android应用)/计算机科学先进技术译丛...

导语内容提要本书作者Nader Dabit是AWS Mobile开发人员、React Native Training创始人和React Native Radio播客主持人。本书旨在帮助iOS、Android和Web开发人员学习使用React Native框架&#xff0c;构建高质量的iOS和Android应用程序。书中介绍了React Native入门基础知识&am…

开发类似vs的黑色风格_传闻:2020年《使命召唤》将是《黑色行动》重启作品

据可信度较高的消息源透露&#xff0c;2020 年的《使命召唤》将是《黑色行动》的重启作。而据之前的报道&#xff0c;《黑色行动》开发商 Treyarch 正在开发今年的《使命召唤》&#xff0c; Sledgehammer Games 和 Raven Software 负责辅助工作。该项目代号为“宙斯”&#xff…

ubuntu中 不同JDK版本之间的切换

Ubuntu中JDK 的切换前提是同时安装了多个版本&#xff0c;如jdk7和jdk8&#xff0c;若要切换&#xff0c;在终端输入&#xff1a; sudo update-alternatives --config javasudo update-alternatives --config javac