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.pswp.cn/news/542372.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542372.shtml
英文地址,請注明出處:http://en.pswp.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