Explore BrainMass
Share

An example of C++ exceptions

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

Suppose an exception, class myException, is defined as follows:

class myException
{
public:
myException()
{
message = "myException thrown!";
cout << "Immediate attention required!"
<< endl;
}
myException (string msg)
{
message = msg;
cout << "Attention required!" << endl;
}
string what()
{
return message;
}
private:
string message;
}

Suppose that in a user program, the catch block has the following form:
catch (myException mE)
{
cout << mE.what() << endl;
}

1. What output will be produced if the exception is thrown with the default constructor?
2. What output will be produced if the exception is thrown with the constructor with parameters with the following actual parameter?
"May Day, May Day"

© BrainMass Inc. brainmass.com March 21, 2019, 5:02 pm ad1c9bdddf
https://brainmass.com/computer-science/cpp/an-example-of-c-exceptions-206676

Solution Preview

Both constructors do two things. They both set the message and they both print a string. Setting the message doesn't cause any output but printing a string does cause a message to be printed as soon as the exception is created. The ...

Solution Summary

This solution shows an example of C++ exceptions in a couple different situations.

$2.19