Something went wrong. Try again.
Java--'s try/catch projects.intra.42.fr/projects/cpp-module-05
Something went wrong. Try again.
9.8 kB · 307 lines
C++
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308#include <exception>#include <iostream>#include <Bureaucrat.hpp>#include <AForm.hpp>#include <ShrubberyCreationForm.hpp>#include <RobotomyRequestForm.hpp>#include <PresidentialPardonForm.hpp>#include <Intern.hpp>
int main() { { Bureaucrat franck; std::cout << franck << std::endl; franck.incrementGrade(); std::cout << franck << std::endl;
Bureaucrat alain("Alain"); std::cout << alain << std::endl; alain.incrementGrade(); std::cout << alain << std::endl;
Bureaucrat james("James", 1); std::cout << james << std::endl; james.decrementGrade(); std::cout << james << std::endl; }
std::cout << "================ Exceptions ================" << std::endl; { std::cout << "=== Decrement too low ===" << std::endl; try { Bureaucrat franckAlainJamesLeboeuf; std::cout << franckAlainJamesLeboeuf << std::endl; franckAlainJamesLeboeuf.decrementGrade(); } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; }
std::cout << "=== Increment too high ===" << std::endl; try { Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 1); std::cout << franckAlainJamesLeboeuf << std::endl; franckAlainJamesLeboeuf.incrementGrade(); } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; }
std::cout << "=== Construct too low ===" << std::endl; try { Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 151); } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; }
std::cout << "=== Construct too high ===" << std::endl; try { Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 0); } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; } }
// std::cout << "================================ Forms ================================" << std::endl; // { // Bureaucrat franck; // Form vente; // std::cout << vente << std::endl; // vente.beSigned(franck); // std::cout << vente << std::endl; // Form vente2; // std::cout << vente2 << std::endl; // franck.signForm(vente2); // std::cout << vente2 << std::endl;
// Bureaucrat alain("Alain"); // Form contrat("Contrat"); // std::cout << contrat << std::endl; // contrat.beSigned(alain); // std::cout << contrat << std::endl; // Form contrat2("Contrat2"); // std::cout << contrat2 << std::endl; // alain.signForm(contrat2); // std::cout << contrat2 << std::endl;
// Bureaucrat james("James", 1); // Form achat("Achat", 1, 1); // std::cout << achat << std::endl; // achat.beSigned(james); // std::cout << achat << std::endl; // Form achat2("Achat2", 1, 1); // std::cout << achat2 << std::endl; // james.signForm(achat2); // std::cout << achat2 << std::endl;
// Bureaucrat franckAlainJamesLeboeuf("Franck Alain James Leboeuf", 1); // Form contratAlternance("Contrat Alternance", 1, 1); // std::cout << contratAlternance << std::endl; // contratAlternance.beSigned(franckAlainJamesLeboeuf); // std::cout << contratAlternance << std::endl; // contratAlternance.beSigned(franckAlainJamesLeboeuf); // std::cout << contratAlternance << std::endl; // Form contractAlternance2("Contrat Alternance 2", 1, 1); // franckAlainJamesLeboeuf.signForm(contractAlternance2); // std::cout << contractAlternance2 << std::endl; // franckAlainJamesLeboeuf.signForm(contractAlternance2); // std::cout << contractAlternance2 << std::endl; // }
// std::cout << "================ Exceptions ================" << std::endl; // { // std::cout << "=== Construct both grade too low ===" << std::endl; // try { // Form vente("Vente", 151, 151); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Construct both grade too high ===" << std::endl; // try { // Form vente("Vente", 0, 0); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Construct to sign grade too low ===" << std::endl; // try { // Form vente("Vente", 151, 150); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Construct to sign grade too high ===" << std::endl; // try { // Form vente("Vente", 0, 0); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Construct to execute grade too low ===" << std::endl; // try { // Form vente("Vente", 150, 151); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Construct to execute grade too high ===" << std::endl; // try { // Form vente("Vente", 150, 0); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Grade to be signed too low ===" << std::endl; // try { // Bureaucrat franck; // Form vente("Vente", 1, 1); // vente.beSigned(franck); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // }
// std::cout << "=== Grade to sign form too low ===" << std::endl; // try { // Bureaucrat franck; // Form vente("Vente", 1, 1); // franck.signForm(vente); // } catch (const std::exception& exception) { // std::cout << exception.what() << std::endl; // } // }
std::cout << "================================ Concrete forms ================================" << std::endl; { std::cout << "=== Shrubbery creation ===" << std::endl; Bureaucrat james("James", 1); ShrubberyCreationForm jardin("Jardin"); std::cout << jardin << std::endl; james.signForm(jardin); std::cout << jardin << std::endl; james.executeForm(jardin);
std::cout << "=== Robotomy request ===" << std::endl; RobotomyRequestForm marvin("Marvin"); std::cout << marvin << std::endl; james.signForm(marvin); std::cout << marvin << std::endl; james.executeForm(marvin); james.executeForm(marvin); james.executeForm(marvin); james.executeForm(marvin);
std::cout << "=== Presidential pardon ===" << std::endl; PresidentialPardonForm arthur("Arthur Dent"); std::cout << arthur << std::endl; james.signForm(arthur); std::cout << arthur << std::endl; james.executeForm(arthur);
std::cout << "=== Execute through base reference ===" << std::endl; PresidentialPardonForm ford("Ford Prefect"); AForm& form = ford; james.signForm(form); james.executeForm(form); }
std::cout << "================ Exceptions ================" << std::endl; { std::cout << "=== Execute form not signed ===" << std::endl; Bureaucrat james("James", 1); PresidentialPardonForm arthur("Arthur Dent"); james.executeForm(arthur);
std::cout << "=== Sign grade too low ===" << std::endl; Bureaucrat franck; ShrubberyCreationForm jardin("Jardin"); franck.signForm(jardin); std::cout << jardin << std::endl;
std::cout << "=== Execute grade too low ===" << std::endl; Bureaucrat alain("Alain", 140); ShrubberyCreationForm maison("Maison"); alain.signForm(maison); std::cout << maison << std::endl; alain.executeForm(maison); james.executeForm(maison);
std::cout << "=== Execute grade boundaries ===" << std::endl; Bureaucrat zaphod("Zaphod", 45); RobotomyRequestForm trillian("Trillian"); zaphod.signForm(trillian); zaphod.executeForm(trillian); zaphod.decrementGrade(); std::cout << zaphod << std::endl; zaphod.executeForm(trillian);
std::cout << "=== Execute not signed then grade too low (direct) ===" << std::endl; try { Bureaucrat leboeuf; PresidentialPardonForm pardon("Franck Leboeuf"); pardon.execute(leboeuf); } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; }
std::cout << "=== Execute grade too low (direct) ===" << std::endl; try { Bureaucrat leboeuf; Bureaucrat president("President", 1); PresidentialPardonForm pardon("Franck Leboeuf"); pardon.beSigned(president); pardon.execute(leboeuf); } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; } }
std::cout << "================================ Intern ================================" << std::endl; { Intern someRandomIntern; Bureaucrat james("James", 1);
std::cout << "=== Robotomy request ===" << std::endl; AForm* rrf; rrf = someRandomIntern.makeForm("robotomy request", "Bender"); std::cout << *rrf << std::endl; james.signForm(*rrf); james.executeForm(*rrf); delete rrf;
std::cout << "=== Shrubbery creation ===" << std::endl; AForm* scf; scf = someRandomIntern.makeForm("shrubbery creation", "Bureau"); std::cout << *scf << std::endl; james.signForm(*scf); james.executeForm(*scf); delete scf;
std::cout << "=== Presidential pardon ===" << std::endl; AForm* ppf; ppf = someRandomIntern.makeForm("presidential pardon", "Ford Prefect"); std::cout << *ppf << std::endl; james.signForm(*ppf); james.executeForm(*ppf); delete ppf; }
std::cout << "================ Exceptions ================" << std::endl; { std::cout << "=== Unknown form name ===" << std::endl; try { Intern someRandomIntern; AForm* form = someRandomIntern.makeForm("coffee request", "Bureau"); delete form; } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; }
std::cout << "=== Empty form name ===" << std::endl; try { Intern someRandomIntern; AForm* form = someRandomIntern.makeForm("", "Bureau"); delete form; } catch (const std::exception& exception) { std::cout << exception.what() << std::endl; } }}