A) write a program illustrating that the order of exception handlers is importan
ID: 3555143 • Letter: A
Question
A) write a program illustrating that the order of exception handlers is important. The first matching handler is the one that executes.
B) Write a program that illustrates a constructor passing information about constructor failure to an exception handler after a try block
C ) Write a program that illustrates rethrowing an exception
D) Write a program that illustrates that a function with its own try block does not have to catch every possible error generated within thr try. Some exceptions can slip through to, and be handled in, outer space
E) Write a program that throws an exception from a deeply nested function and still has the catch handler following the try block enclosing the initial call in main catch the exception
Explanation / Answer
Order of catching (C++ only)
If the compiler encounters an exception in a try block, it will try each handler in order of appearance.
If a catch block for objects of a base class precedes a catch block for objects of a class derived from that base class, the compiler issues a warning and continues to compile the program despite the unreachable code in the derived class handler.
A catch block of the form catch(...) must be the last catch block following a try block or an error occurs. This placement ensures that the catch(...)block does not prevent more specific catch blocks from catching exceptions intended for them.
If the run time cannot find a matching handler in the current scope, the run time will continue to find a matching handler in a dynamically surrounding try block. The following example demonstrates this:
#include <iostream>
using std::cout;
using std::cerr;
class InvalidIDNumberError {
public:
InvalidIDNumberError( char *s ) : errorMessage( s ) {}
void print() const { cerr << errorMessage; }
private:
char *errorMessage;
};
class TestInvalidIDNumberError {
public:
TestInvalidIDNumberError( int id ) : idNumber( id )
{
cout << "Constructor for object " << idNumber << ' ';
if ( idNumber < 0 )
throw InvalidIDNumberError( "ERROR: Negative ID number" );
}
private:
int idNumber;
};
int main()
{
try {
TestInvalidIDNumberError valid( 10 ), invalid( -1 );
}
catch ( InvalidIDNumberError &error ) {
error.print();
cerr << ' ';
}
return 0;
}
#include <iostream>
using std::cout;
using std::cerr;
class TestException {
public:
TestException( char *m ) : message( m ) {}
void print() const { cout << message << ' '; }
private:
char *message;
};
void f() { throw TestException( "Test exception thrown" ); }
void g()
{
try {
f();
}
catch ( ... ) {
cerr << "Exception caught in function g(). Rethrowing... ";
throw;
}
}
int main()
{
try {
g(); // start function call chain
}
catch ( ... ) {
cerr << "Exception caught in function main() ";
}
return 0;
}
#include <iostream>
using std::cout;
using std::cerr;
class TestException1 {
public:
TestException1( char *m ) : message( m ) {}
void print() const { cerr << message << ' '; }
private:
char *message;
};
class TestException2 {
public:
TestException2( char *m ) : message( m ) {}
void print() const { cout << message << ' '; }
private:
char *message;
};
void f()
{
throw TestException1( "TestException1" );
}
void g()
{
try {
f();
}
catch ( TestException2 &t2 ) {
cerr << "In g: Caught ";
t2.print();
}
}
int main()
{
try {
g();
}
catch ( TestException1 &t1 ) {
cerr << "In main: Caught ";
t1.print();
}
return 0;
}
#include <iostream>
using std::cout;
using std::cerr;
class TestException {
public:
TestException ( char *m ) : message( m ) {}
void print() const { cerr << message << ' '; }
private:
char *message;
};
void f() { throw TestException( "TestException" ); }
void g() { f(); }
void h() { g(); }
int main()
{
try {
h();
}
catch ( TestException &t ) {
cerr << "In main: Caught ";
t.print();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.