Question 1
#include <iostream>
using namespace std;
int main()
{
int x = -1;
try {
cout << \"Inside try \\n\";
if (x < 0)
{
throw x;
cout << \"After throw \\n\";
}
}
catch (int x ) {
cout << \"Exception Caught \\n\";
}
cout << \"After catch \\n\";
return 0;
}
Question 2
Question 3
What should be put in a try block?
1. Statements that might cause exceptions 2. Statements that should be skipped in case of an exception
Question 4
#include<iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Derived d;
try {
throw d;
}
catch(Base b) {
cout<<\"Caught Base Exception\";
}
catch(Derived d) {
cout<<\"Caught Derived Exception\";
}
return 0;
}
Question 5
#include <iostream>
using namespace std;
int main()
{
try
{
throw \'a\';
}
catch (int param)
{
cout << \"int exception\\n\";
}
catch (...)
{
cout << \"default exception\\n\";
}
cout << \"After Exception\";
return 0;
}
Question 6
#include <iostream>
using namespace std;
int main()
{
try
{
throw 10;
}
catch (...)
{
cout << \"default exception\\n\";
}
catch (int param)
{
cout << \"int exception\\n\";
}
return 0;
}
Question 7
#include <iostream>
using namespace std;
int main()
{
try
{
try
{
throw 20;
}
catch (int n)
{
cout << \"Inner Catch\\n\";
throw;
}
}
catch (int x)
{
cout << \"Outer Catch\\n\";
}
return 0;
}
Question 8
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << \"Constructing an object of Test \" << endl; }
~Test() { cout << \"Destructing an object of Test \" << endl; }
};
int main() {
try {
Test t1;
throw 10;
} catch(int i) {
cout << \"Caught \" << i << endl;
}
}
Question 9
#include <iostream>
using namespace std;
class Test {
static int count;
int id;
public:
Test() {
count++;
id = count;
cout << \"Constructing object number \" << id << endl;
if(id == 4)
throw 4;
}
~Test() { cout << \"Destructing object number \" << id << endl; }
};
int Test::count = 0;
int main() {
try {
Test array[5];
} catch(int i) {
cout << \"Caught \" << i << endl;
}
}
Question 10
void fun(int a, char b) throw (Exception1, Exception2, ..)
There are 12 questions to complete.