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