Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 12c SQL

Authors: Joan Casteel

3rd edition

1305251032, 978-1305251038

More Books

Students also viewed these Databases questions