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.\n";
}
else{
cout<<"Product of "<<a<<" and "<<b<<" is: "<<a*b<<endl;
}
}
int main()
{
try{
func(5,0);
}
catch(const char* e){
cout<<e;
}
}
#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.\n";
}
else{
cout<<"Product of "<<a<<" and "<<b<<" is: "<<a*b<<endl;
}
}
int main()
{
try{
func(5,0);
}
catch(const char* e){
cout<<e;
}
}A. 0
B. 5
C. This value of b will make the product zero. So please provide positive values.
D. Product of 5 and 0 is: 0
Answer: Option C
Related Questions on Exception Handling in C plus plus
What is exception handling in C++?
A. A method to handle errors during runtime
B. A method to handle errors during compile time
C. A method to handle errors during linking
D. A method to handle errors during execution
What is the purpose of the 'try' block in exception handling in C++?
A. To catch exceptions
B. To throw exceptions
C. To define the block of code that may generate exceptions
D. To handle exceptions
Which of the following is not a standard exception class in C++?
A. IOException
B. std::exception
C. std::runtime_error
D. std::out_of_range

Join The Discussion