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”。 我們在num1 , num2和res中聲明了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”。 我們在num1 , num2和res中聲明了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.
在這里,我們定義了一個帶有兩個參數X和Y的 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”塊中聲明了局部變量num1和num2 ,并調用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.
在這里,我們定義了一個帶有兩個參數X和Y的 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”塊中聲明了局部變量num1和num2 ,并調用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 ++查找字符串