My assignment for my cs class is to create two custom exception classes inheriting from std::logic_error: OverflowingSwimmingPoolException and UnderflowingSwimmingPoolException. When an illegal operation is attempted, create and throw a custom exception, rather than just printing an error message. Include try...catch blocks in your driver code to catch any exceptions.
this is the part of my header file:
#ifndef SWIMMINGPOOL_H
#define SWIMMINGPOOL_H
#include <stdexcept>
#include <iostream>
using namespace std;
namespace cs52
{
class OverflowingSwimmingPoolException: public logic_error
{
OverflowingSwimmingPoolException (){};
};
class UnderflowingSwimmingPoolException: public logic_error
{
UnderflowingSwimmingPoolException(){};
};
here is the compiler says on the line where constructor is: Constructor for 'cs52::UnderflowingSwimmingPoolException' must explicitly initialize the base class 'std::logic_error' which does not have a default constructor.
That is what I have in my implementation file:
#include "SwimmingPool.h"
#include <stdexcept>
#include <iostream>
using namespace std;
namespace cs52
{
SwimmingPool operator +(const SwimmingPool& pool1, const SwimmingPool& pool2) throw (OverflowingSwimmingPoolException, UnderflowingSwimmingPoolException)
{
SwimmingPool temp;
temp.mySize = pool1.mySize+pool2.mySize;
temp.myContents = pool1.myContents+pool2.myContents;
if (temp.myContents>temp.mySize)
throw OverflowingSwimmingPoolException();
return temp;
}
SwimmingPool operator -(const SwimmingPool& pool1, const SwimmingPool& pool2) throw (OverflowingSwimmingPoolException, UnderflowingSwimmingPoolException)
{
SwimmingPool temp;
temp.mySize= pool1.mySize-pool2.mySize;
temp.myContents= pool1.myContents-pool2.myContents;
if (temp.myContents>temp.mySize)
throw OverflowingSwimmingPoolException();
if (temp.myContents<0 || temp.mySize<0)
throw UnderflowingSwimmingPoolException();
return temp;
}
}
the compiler shows the error in the line where I throw the exception class. it says: calling a private constructor of class cs53:OverflowingSwimmimgPoolException.
and the part of my driver file should look something like that:
using namespace cs52;
try {
SwimmingPool badPool = smallOne - bigOne;
cout << "This should never happen..." << endl;
}
catch( UnderflowingSwimmingPoolException uspe ) {
cout << "working..." << endl;
}
catch( OverflowingSwimmingPoolException uspe ) {
cout << "This should never happen..." << endl;
}
I have just started to code, so I do not really understand how the classes like std::logic_error that are already created in the libraries work.
In your constructor for the derived exception, you must call the constructor of the base class, which takes a string containing some error text, like this:
This will then be returned when you call
what()
on the caught exception (assuming you don't override it with different behaviour; but don't do that):It is common for the interface of an exception type to take a string for this purpose, but of course for your own exception types, you don't have to.
Note that if you
catch
the exception using the base class, make sure you catch by reference (or reference-to-const
), to avoid object slicing.