(Catching Derived-Class Exceptions) Use inheritance to create various derived classes of runtime_error. Then show that a catch handler specifying the base class can catch derived-class exceptions.

What will be an ideal response?


```
#include
#include
using namespace std;

// class DerivedException1 definition
class DerivedException1 : public runtime_error
{
public:
// constructor specifies error message
DerivedException1::DerivedException1( const string& message )
: runtime_error( message ) {}
}; // end class DerivedException1
```
```
#include
#include
using namespace std;

// class DerivedException2 definition
class DerivedException2 : public runtime_error
{
public:
// constructor specifies error message
DerivedException2::DerivedException2( const string& message )
: runtime_error( message ) {}
}; // end class DerivedException2
```
```
#include
#include "DerivedException1.h"
#include "DerivedException2.h"
using namespace std;

int main()
{
try // throw DerivedException1
{
throw ( DerivedException1( "DerivedException1" ) );
} // end try
catch ( runtime_error &exception ) // exceptions of runtime_error
{
cerr << exception.what() << endl;
} // end catch

try // throw DerivedException2
{
throw ( DerivedException2( "DerivedException2" ) );
} // end try
catch ( runtime_error &exception ) // exceptions of runtime_error
{
cerr << exception.what() << endl;
} // end catch

return 0;
} // end main
```
DerivedException1
DerivedException2

Computer Science & Information Technology

You might also like to view...

What numbers are displayed in the list box by the following program segment?

``` Dim numbers() As Integer = {4, 7, 9, 3, 1, 7, 7} Dim query = From number in numbers Where number > 6 Select number Distinct lstBox.Items.Add(query.Count) lstBox.Items.Add(query.Average) ``` (A) 5 and 12 (B) 2 and 12 (C) 2 and 8 (D) 5 and

Computer Science & Information Technology

The ____ value for the content model in an element declaration means the element can contain both parsed character data and child elements.

A. mixed B. Elements C. #PCDATA with sequence D. BOTH

Computer Science & Information Technology

Which of the following most often includes a touch pad, track ball, or small pointing stick?

A) smartphone B) laptop C) desktop D) microcomputer

Computer Science & Information Technology

________ is temporary memory

A) CMOS B) USB C) RAM D) BIOS

Computer Science & Information Technology