Skip to content

calendar.h ~ exceptions

Baptiste Thémine edited this page Jul 5, 2020 · 4 revisions
struct exception;

Description

Provides a set of exceptions used within class Date, class Interval and class Period.

An exception is a an object to be thrown inside a function in order to report an error i.e. a violation of preconditions. class Date, class Interval and class Period both have invariants and their construction/manipulation throws the exceptions described below. These exceptions are designed to be catchable with their equivalent std exception.

Requirements

<cstdio>, <cstdlib>, <iostream>, <stdexcept>, <JLC/debug.h> and <JLC/functions.h> must be included.

Hierarchy

calendar::exception
|
+-- date::exception
|   |
|   +-- date::invalid_format
|   |
|   \-- date::not_a_date
|
+-- interval::exception
|   |
|   +-- interval::empty_interval
|   |
|   \-- interval::not_an_interval
|
\-- period::exception
    |
    +-- period::invalid_format
    |
    \-- period::not_a_period

Exception details

Name STD equivalent Description
calendar::exception Defines generic abstract type for all calendar exceptions.
date::exception Defines generic abstract type for class Date exceptions.
date::invalid_format std::invalid_argument Reports errors that are consequence of attempt to create a class Date object with an invalid specified format string.
date::not_a_date std::invalid_argument Reports errors that are consequence of class Date invariant violations.
interval::exception Defines generic abstract type for class Interval exceptions.
interval::empty_interval std::domain_error Reports errors that are consequence of attempt to access class Interval properties while being empty.
interval::not_an_interval std::invalid_argument Reports errors that are consequence of class Interval invariant violations.
period::exception Defines generic abstract type for class Period exceptions.
period::invalid_format std::invalid_argument Reports errors that are consequence of attempt to create a class Period object with an invalid specified format string.
period::not_a_period std::invalid_argument Reports errors that are consequence of class Period invariant violations.

Member functions

exception(const std::string &msg);           //(1)
virtual ~exception() noexcept;               //(2)
virtual const char* what() const noexcept;   //(3)
  1. Constructor --> Builds exception object with the specified explanatory message.
  2. Destructor
  3. Accessor --> Returns the explanatory message.

Example

The following example shows different ways to catch class Date exceptions from the most specific to the most generic.

#include <JLC/JLC.h>
using namespace JLC;

int main(){
    //A date with year = 0 is impossible
    try{
        constexpr Date date = Date(0);}
    //From the most specific exception...
    catch(const date::not_a_date &error){
        std::cout << error.what() << std::endl;}
    catch(const date::exception &error){
        std::cout << error.what() << std::endl;}
    catch(const calendar::exception &error){
        std::cout << error.what() << std::endl;}
    catch(const std::invalid_argument &error){
        std::cout << error.what() << std::endl;}
    //... to the most generic exception
    catch(const std::exception &error){
        std::cout << error.what() << std::endl;}
}

See also

Clone this wiki locally