Exception Handling In C Plus Plus - Study Mode

[#66] What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstdlib>
using namespace std
class A
{
int a
public:
A(){}
}
class B: public A
{
int b
public:
B(){}
}
void func1()
{
B b
throw b
}

void func2()
{
A a
throw a
}

int main()
{
try{
func1()
}
catch(...){
cout<<"Caught All types of exceptions
"
}
try{
func2()
}
catch(B b){
cout<<"Caught All types of exceptions
"
}
}
Correct Answer

(B) Caught All types of exceptions Aborted (core dumped)

[#67] What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstdlib>
using namespace std
void func(int a, int b)
{
if(b == 0){
throw "This value of b will make the product zero. "
"So please provide positive values.
"
}
else{
cout<<"Product of "<<a<<" and "<<b<<" is: "<<a*b<<endl
}
}

int main()
{
try{
func(5,0)
}
catch(char* e){
cout<<e
}
}
Correct Answer

(B) Aborted (core dumped)

[#68] What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstdlib>
using namespace std
void func(int a, int b)
{

if(b == 0){
throw "This value of b will make the product zero. "
"So please provide positive values.
"
}
else{
cout<<"Product of "<<a<<" and "<<b<<" is: "<<a*b<<endl
}
}

int main()
{
try{
func(5,0)
}
catch(const char* e){
cout<<e
}
}
Correct Answer

(C) This value of b will make the product zero. So please provide positive values.