Catching Exceptions(C++ Programming ) Questions and Answers
Explanation:-
Answer: Option B. -> 2.236We are finding the square root of the number, if it is a positive number, it can manipulate, Otherwise
it will arise a exception.
Output:
$ g++ goe4.cpp
$ a.out
2.236
Question 4.
1.
#include
2.
#include
3.
using namespace std;
4.
int main()
5.
{
6.
double Op1 = 10, Op2 = 5, Res;
7.
char Op;
8.
try
9.
{
10.
if (Op != '+' && Op != '-' && Op != '*' && Op != '/')
11.
throw Op;
12.
switch(Op)
13.
{
14.
case '+':
15.
Res = Op1 + Op2;
16.
break;
17.
case '-':
18.
Res = Op1 - Op2;
19.
break;
20.
case '*':
21.
Res = Op1 * Op2;
22.
break;
23.
case '/':
24.
Res = Op1 / Op2;
25.
break;
26.
}
27.
cout
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
int main()
5.
{
6.
double Op1 = 10, Op2 = 5, Res;
7.
char Op;
8.
try
9.
{
10.
if (Op != '+' && Op != '-' && Op != '*' && Op != '/')
11.
throw Op;
12.
switch(Op)
13.
{
14.
case '+':
15.
Res = Op1 + Op2;
16.
break;
17.
case '-':
18.
Res = Op1 - Op2;
19.
break;
20.
case '*':
21.
Res = Op1 * Op2;
22.
break;
23.
case '/':
24.
Res = Op1 / Op2;
25.
break;
26.
}
27.
cout
Explanation:-
Answer: Option D. -> is not a valid operatorIt will arise a exception because we missed a operator.
Output:
$ g++ goe3.cpp
$ a.out
is not a valid operator
Explanation:-
Answer: Option C. -> Division by zero condition!We are dividing the values and if one of the values is zero means, We are arising an exception.
Output:
$ g++ goe2.cpp
$ a.out
Division by zero condition!
Explanation:-
Answer: Option A. -> 5In this program, We are checking the age of a person, If it is zero means, We will arise a exception.
Output:
$ g++ goe1.cpp
$ a.out
5
Explanation:-
Answer: Option A. -> exception arisedIn this program, We are arising a standard exception and catching that and returning a statement.
Output:
$ g++ goe.cpp
$ a.out
exception arised