Module 05 - Marymota/CPP_Modules GitHub Wiki

Ex00

Exceptions

Throwing exceptions is a mechanism to handle errors that my occur at run-time. In the context of this exercise, we will be throwing exceptions to report failures of constructors when or parameters range is not met.

Exception handling is a process of throwing and catching an exception by searching the call chain for a handler from the throw point up through its callers. If a function throws an exception for which there is no matching exception handler the program will be terminated. (This is considered a compiler error and must be avoid).

I'll be applying the library that contains a class that helps applying exceptions. In our header file we create a class for each different exception we need to throw that will inherit from the class std::exception.

class myException : public std::exception {
   public:
      const char* what() const throw(); // The function std::what() returns a null character sequence that is used to identify the exception.
};

Then, we can define the error message that will be sent when this exception is thrown:

const char* ExceptionalClass::myException::what() const throw () {
   return "Descriptive error message";
}

To throw an exception, we just need to use the _throw _keyword followed by 'myException()' in the cases it may occur:

ExceptionalClass::ExceptionalClass(int age) // Needs to be older than 18
   if (age < 18)
      throw myException();
}

Now we can prevent an object with age less than 18 to be created using the try and catch keywords when calling the constructor:

try {
   ExceptionalClass human(13)  
} catch (const std::exception e){
   std::cerr << e. what() << std::endl;
}

// output: Descriptive error message

Resources:
Exceptions
exception::what() in C++ with Examples


Ex02

Virtual functions

Now, add the execute(Bureaucrat const & executor) const member function to the base form and implement a function to execute the form’s action of the concrete classes. You have to check that the form is signed and that the grade of the bureaucrat attempting to execute the form is high enough. Otherwise, throw an appropriate exception.

At this point we already turned the Form class into an abstract class by defining at least one pure virtual function virtual ~Form() = 0 A pure virtual function must be one that will not be incorporated by a derived class because they are unusable by them. By turning Form into an abstract class we already guarantee that there will be no conflicts between functions with the same name in the inheritance scheme (i.e. execute(Bureaucrat const & executor) const).

To call the executor method of derived classes we turn the one in Form virtual and so we can't use it. The result is that any exceptions we want to verify need to be done in each of the classes. The alternative is to create a different function to do form executions, and to use or principal Form::execution() to check grades validity.

⚠️ **GitHub.com Fallback** ⚠️