From b3611b2a2a4500136ba5fe0466f5313c53a86bbe Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 28 Mar 2025 16:19:14 +0100 Subject: [PATCH] adding the try/catch sur new (useless) --- cpp06/ex02/includes/Base.hpp | 7 ++++++- cpp06/ex02/main.cpp | 27 +++++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/cpp06/ex02/includes/Base.hpp b/cpp06/ex02/includes/Base.hpp index 8634cc3..f636030 100644 --- a/cpp06/ex02/includes/Base.hpp +++ b/cpp06/ex02/includes/Base.hpp @@ -6,13 +6,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 #include +#include +#include +#include +#include +#include class Base { public: diff --git a/cpp06/ex02/main.cpp b/cpp06/ex02/main.cpp index c207c10..a21cdf8 100644 --- a/cpp06/ex02/main.cpp +++ b/cpp06/ex02/main.cpp @@ -6,7 +6,7 @@ /* 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 "C.hpp" -#include -#include -#include -#include #include "color.hpp" /** @@ -34,19 +30,34 @@ Base* generate(void) { 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; - 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; } 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; - ptr = new B; + try { + 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; } 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; - ptr = new C; + try { + 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; } default: