To where does the program control transfer when an exception is raised in C++?
Which keyword is used to check exception in the block of code in C++?
What will happen when the exception is not caught in the program in C++?
What is the output of this C++ program?
#include <iostream>
using namespace std;
int main()
{
int age = 0;
try
{
if (age < 0)
{
throw "Positive Number Required";
}
cout << age;
}
catch(const char *Message)
{
cout << "Error: " << Message;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
using namespace std;
void PrintSequence(int StopNum)
{
int Num;
Num = 1;
while (true)
{
if (Num >= StopNum)
throw Num;
cout << Num;
Num++;
}
}
int main(void)
{
try
{
PrintSequence(20);
}
catch(int ExNum)
{
cout << "Caught an exception with value: " << ExNum;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
using namespace std;
double division(int a, int b)
{
if (b == 0)
{
throw "Division by zero condition!";
}
return (a / b);
}
int main ()
{
int x = 50;
int y = 2;
double z = 0;
try
{
z = division(x, y);
cout << z;
}
catch(const char *msg)
{
cerr << msg;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
using namespace std;
int main()
{
char* buff;
try
{
buff = new char[1024];
if (buff == 0)
throw "Memory allocation failure!";
else
cout << sizeof(buff) << " Bytes allocated successfully"<<endl;
}
catch(char *strg)
{
cout<<"Exception raised: "<<strg<<endl;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
using namespace std;
void Funct();
int main()
{
try
{
Funct();
}
catch(double)
{
cerr << "caught a double type..." << endl;
}
return 0;
}
void Funct()
{
throw 3;
}
What is the output of this C++ program?
#include <iostream>
#include <exception>
using namespace std;
int main()
{
try
{
int * array1 = new int[100000000];
int * array2 = new int[100000000];
int * array3 = new int[100000000];
int * array4 = new int[100000000];
cout << "Allocated successfully";
}
catch(bad_alloc&)
{
cout << "Error allocating the requested memory." << endl;
}
return 0;
}
In C++, what will happen if the handler is not found for exception?
In C++, which keyword is used to handle the expection?
In C++, which keyword is used to throw an exception?
What is the output of this C++ program?
#include <iostream>
using namespace std;
double division(int a, int b)
{
if (b == 0)
{
throw "Division by zero condition!";
}
return (a / b);
}
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try
{
z = division(x, y);
cout << z << endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
using namespace std;
int main ()
{
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred " << e << endl;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
#include <exception>
using namespace std;
class myexception: public exception
{
virtual const char* what() const throw()
{
return "My exception";
}
} myex;
int main ()
{
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
#include <exception>
using namespace std;
int main ()
{
try
{
int* myarray = new int[1000];
cout << "Allocated";
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception
{
const char * what () const throw ()
{
return "C++ Exception";
}
};
int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
cout << "Exception caught" << std::endl;
cout << e.what() << std::endl;
}
catch(std::exception& e)
{
}
}
How do we define the user-defined exceptions in C++?
In C++, which exception is thrown by dynamic_cast?
In C++, how many parameters does the throw expression can have?
Where are the exceptions handled in C++?
What is the output of this C++ program?
#include <iostream>
#include <exception>
using namespace std;
class myexception: public exception
{
virtual const char* what() const throw()
{
return "Exception arised";
}
} myex;
int main ()
{
try
{
throw myex;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
using namespace std;
int main()
{
int age=5;
try
{
if (age < 0)
throw "Positive Number Required";
cout << age << "\n\n";
}
catch(const char* Message)
{
cout << "Error: " << Message;
}
return 0;
}
What is the output of this C++ program?
#include <iostream>
#include <string>
using namespace std;
int main()
{
double Op1 = 10, Op2 = 5, Res;
char Op;
try
{
if (Op != '+' && Op != '-' && Op != '*' && Op != '/')
throw Op;
switch(Op)
{
case '+':
Res = Op1 + Op2;
break;
case '-':
Res = Op1 - Op2;
break;
case '*':
Res = Op1 * Op2;
break;
case '/':
Res = Op1 / Op2;
break;
}
cout << "\n" << Op1 << " " << Op << " "<< Op2 << " = " << Res;
}
catch (const char n)
{
cout << n << " is not a valid operator";
}
return 0;
}
What is the output of this C++ program?
#include<iostream>
#include "math.h"
using namespace std;
double MySqrt(double d)
{
if (d < 0.0)
throw "Cannot take sqrt of negative number";
return sqrt(d);
}
int main()
{
double d = 5;
cout << MySqrt(d) << endl;
}