(Constructors Throwing Exceptions) Write a program that shows a constructor passing in- formation about constructor failure to an exception handler after a try block.
What will be an ideal response?
```
#include
#include
using namespace std;
// class InvalidIDException definition
class InvalidIDException : public runtime_error
{
// constructor specifies error message
InvalidIDException::InvalidIDException( const string &message )
: runtime_error( message ) {}
}; // end class InvalidIDException
```
```
class IDObject
{
public:
IDObject( int ); // constructor
private:
int idNumber;
}; // end class IDObject
```
```
#include
#include "IDObject.h"
#include "InvalidIDException.h"
using namespace std;
// constructor takes an int
IDObject::IDObject( int id ) : idNumber( id )
{
cout << "Constructor for IDObject " << idNumber << '\n';
if ( idNumber < 0 ) // negative id
throw InvalidIDException( "ERROR: Negative ID number" );
} // end IDObject constructor
```
```
#include
#include "IDObject.h"
#include "InvalidIDException.h"
using namespace std;
int main()
{
try // create two IDObjects
{
IDObject valid( 10 ); // valid ID
IDObject invalid( -1 ); // invalid ID, will throw an exception
} // end try
catch ( InvalidIDException &exception ) // catch InvalidIDException
{
cerr << exception.what() << '\n';
} // end catch
return 0;
} // end main
```
Constructor for IDObject 10
Constructor for IDObject -1
ERROR: Negative ID number
You might also like to view...
In the following code segment, what type of variable is counter?
``` Dim temp, counter, check As Integer Do temp = CInt(InputBox("Enter a number.")) counter += temp If counter = 10 Then check = 0 End If Loop Until (check = 0) ``` (A) counter (B) accumulator (C) sentinel (D) loop control variable
Which of the following is an advantage of implementing a queue using an array?
a. It is more efficient if the exact size of the input is known upfront. b. It prioritizes the elements in the queue. c. It does not have to keep a head and a tail pointer. d. It is not restricted by a static size.
The supervisor program of the operating system is also known as the ________
Fill in the blank(s) with correct word
Briefly describe "6over4" addresses.
What will be an ideal response?