Question: throw ( rand ( ) % 2 ? DerivedException ( DerivedException ) : DerivedException 2 ( DerivedException 2 ) ) ; } catch (

throw ( rand()%2? DerivedException( "DerivedException" ) :
DerivedException2( "DerivedException2"));
}
catch ( BaseException &b ){
b.print();
}
return 0;
}C. Write a program which shows that all destructors for objects constructed in a block are
called before an exception is thrown from that block.using std::cout;
using std::cerr;
class Object {
public:
Object( int val ): value( val )
{ cout "Object " value " constructor
"; }
\sim Object()
{ cout "Object " value " destructor
"; }
private:
int value;
};
class Error {
public:
Error( char *s ) : string( s ){}
private:
char *string;
};
int main()
{
try {
Object a(1), b(2), c(3);
cout '
';
throw Error( "This is a test exception" );
}
catch ( Error &e ){
e.print();
}
return 0;
}A. Suppose a program throws an exception and the appropriate exception handler begins
executing. Now suppose that the exception handler itself throws the same exception. Does this
create an infinite recursion? Write a program in C++ to check your observation.using std::cout;
class TestException {
public:
TestException( char *mPtr ) : message( mPtr ){}
private:
char *message;
};
int main()
{
try {
throw TestException( "This is a test" );
}
catch ( TestException &t ){
t.print();
throw TestException( "This is another test" );
}
return 0;
}B. Use inheritance to create a base exception class and various derived exception classes. Then
show that a catch handler specifying the base class can catch derived-class exceptions.using std::cout;
#include
 throw ( rand()%2? DerivedException( "DerivedException" ) : DerivedException2( "DerivedException2")); } catch

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!