adding the try/catch sur new (useless)

This commit is contained in:
Raphael 2025-03-28 16:19:14 +01:00
parent 4e72caefbe
commit b3611b2a2a
2 changed files with 25 additions and 9 deletions

View file

@ -6,13 +6,18 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/26 12:07:18 by rparodi #+# #+# */ /* Created: 2025/03/26 12:07:18 by rparodi #+# #+# */
/* Updated: 2025/03/26 12:44:36 by rparodi ### ########.fr */ /* Updated: 2025/03/28 16:11:50 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <new>
#include <typeinfo>
class Base { class Base {
public: public:

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/26 11:58:38 by rparodi #+# #+# */ /* Created: 2025/03/26 11:58:38 by rparodi #+# #+# */
/* Updated: 2025/03/26 14:10:19 by rparodi ### ########.fr */ /* Updated: 2025/03/28 16:18:27 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,10 +14,6 @@
#include "B.hpp" #include "B.hpp"
#include "C.hpp" #include "C.hpp"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <typeinfo>
#include "color.hpp" #include "color.hpp"
/** /**
@ -34,19 +30,34 @@ Base* generate(void) {
case 0: case 0:
{ {
std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and A it's chosen ]" << CLR_RESET << std::endl << std::endl; std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and A it's chosen ]" << CLR_RESET << std::endl << std::endl;
ptr = new A; try {
ptr = new (std::nothrow) A;
} catch (const std::bad_alloc &err) {
std::cerr << CLR_RED << "Error: " << err.what() << " " << __FILE_NAME__ << ":" << __LINE__ << CLR_RESET << std::endl;
exit(1);
}
break; break;
} }
case 1: case 1:
{ {
std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and B it's chosen ]" << CLR_RESET << std::endl << std::endl; std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and B it's chosen ]" << CLR_RESET << std::endl << std::endl;
try {
ptr = new B; ptr = new B;
} catch (const std::bad_alloc &err) {
std::cerr << CLR_RED << "Error: " << err.what() << " " << __FILE_NAME__ << ":" << __LINE__ << CLR_RESET << std::endl;
exit(1);
}
break; break;
} }
case 2: case 2:
{ {
std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and C it's chosen ]" << CLR_RESET << std::endl << std::endl; std::cout << std::endl << CLR_YELLOW << "\t[ Chosen has a value of " << CLR_GOLD << choice << CLR_YELLOW << " and C it's chosen ]" << CLR_RESET << std::endl << std::endl;
try {
ptr = new C; ptr = new C;
} catch (const std::bad_alloc &err) {
std::cerr << CLR_RED << "Error: " << err.what() << " " << __FILE_NAME__ << ":" << __LINE__ << CLR_RESET << std::endl;
exit(1);
}
break; break;
} }
default: default: