From cf7465d5f99ef602b95bcdd7a03512b86056b35f Mon Sep 17 00:00:00 2001 From: Pradeep Dhupar <148972812+PradeepITT@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:23:18 +0530 Subject: [PATCH 1/3] Added assignment for try catch --- Try_Catch.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Try_Catch.cpp diff --git a/Try_Catch.cpp b/Try_Catch.cpp new file mode 100644 index 0000000..2fa6710 --- /dev/null +++ b/Try_Catch.cpp @@ -0,0 +1,73 @@ +#include +#include + +using namespace std; + +class CarDamageException : public exception { +private: + string message; +public: + CarDamageException(const char* msg) : message(msg) {} + const char* what() const noexcept override { + return message.c_str(); + } +}; + +class BrakeFailureException : public exception { +private: + string message; +public: + BrakeFailureException(const char* msg) : message(msg) {} + const char* what() const noexcept override { + return message.c_str(); + } +}; + +class Car { +public: + void driveSafely(bool collision, bool brakeFailure) { + if (collision) { + throw CarDamageException("Car damaged due to collision."); + } else if (brakeFailure) { + throw BrakeFailureException("Brake Failure Occurred"); + } + } +}; + +class AccidentHandler { +public: + static void handleCarDamage(const char* message) { + cout << "Car damaged: " << message << ". Insurance claim process initiated." << endl; + } + + static void handleBrakeFailure(const char* message) { + cout << "Brake failure detected: " << message << ". Pull over safely and engage emergency brake." << endl; + } + + static void cleanUpAfterAccident() { + cout << "Clean-up operations after accident completed." << endl; + } +}; + +class CarAccident { +public: + static void main() { + Car car; + try { + car.driveSafely(false, true); + cout << "Both persons reached the destination and are safe." << endl; + } catch (CarDamageException& e) { + AccidentHandler::handleCarDamage(e.what()); + } catch (BrakeFailureException& e) { + AccidentHandler::handleBrakeFailure(e.what()); + } catch (...) { + cerr << "Unknown exception occurred." << endl; + } + AccidentHandler::cleanUpAfterAccident(); + } +}; + +int main() { + CarAccident::main(); + return 0; +} From acb2c9a665c1f529ea83dc11638bf73f044e9cd4 Mon Sep 17 00:00:00 2001 From: Pradeep Dhupar <148972812+PradeepITT@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:29:49 +0530 Subject: [PATCH 2/3] Created Sperate files. --- BrakeFailureException.cpp | 9 ++++++++ CarAccident.cpp | 46 ++++++++++++++++++++++++++++++++++++++ CarDamageException.cpp | 9 ++++++++ Main.cpp | 9 ++++++++ PersonInjuredException.cpp | 8 +++++++ 5 files changed, 81 insertions(+) create mode 100644 BrakeFailureException.cpp create mode 100644 CarAccident.cpp create mode 100644 CarDamageException.cpp create mode 100644 Main.cpp create mode 100644 PersonInjuredException.cpp diff --git a/BrakeFailureException.cpp b/BrakeFailureException.cpp new file mode 100644 index 0000000..94c4918 --- /dev/null +++ b/BrakeFailureException.cpp @@ -0,0 +1,9 @@ +#include +#include +using namespace std; + +class BrakeFailureException : public exception { +public: + BrakeFailureException(const char* message) : exception(message) {} +}; + diff --git a/CarAccident.cpp b/CarAccident.cpp new file mode 100644 index 0000000..6ff3e9c --- /dev/null +++ b/CarAccident.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace std; + +class CarAccident { +public: + static void main() { + try { + driveSafely(); + cout << "Both persons reached the destination and are safe." << endl; + } catch (CarDamageException& e) { + handleCarDamage(); + } catch (PersonInjuredException& e) { + handlePersonInjured(); + } catch (BrakeFailureException& e) { + handleBrakeFailure(); + } catch (...) { + cerr << "Unknown exception occurred." << endl; + } + } + +private: + static void driveSafely() { + bool collision = false; + bool brakeFailure = true; + + if (collision) { + throw CarDamageException("Car damaged due to collision."); + } else if (brakeFailure) { + throw BrakeFailureException("Brake Failure Occurred"); + } + } + + static void handleCarDamage() { + cout << "Car damaged due to collision. Insurance claim process initiated." << endl; + } + + static void handlePersonInjured() { + cout << "Person injured due to collision. Medical assistance required." << endl; + } + + static void handleBrakeFailure() { + cout << "Brake failure detected. Pull over safely and engage emergency brake." << endl; + } +}; \ No newline at end of file diff --git a/CarDamageException.cpp b/CarDamageException.cpp new file mode 100644 index 0000000..01fa4c9 --- /dev/null +++ b/CarDamageException.cpp @@ -0,0 +1,9 @@ +#include +#include + +using namespace std; + +class CarDamageException : public exception { +public: + CarDamageException(const char* message) : exception(message) {} +}; \ No newline at end of file diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..67627e7 --- /dev/null +++ b/Main.cpp @@ -0,0 +1,9 @@ +#include +#include + +using namespace std; + +int main() { + CarAccident::main(); + return 0; +} diff --git a/PersonInjuredException.cpp b/PersonInjuredException.cpp new file mode 100644 index 0000000..aba38c9 --- /dev/null +++ b/PersonInjuredException.cpp @@ -0,0 +1,8 @@ +#include +#include +using namespace std; + +class PersonInjuredException : public exception { +public: + PersonInjuredException(const char* message) : exception(message) {} +}; \ No newline at end of file From 1e09ebef84192607a9c906b0c4a8f19a26a89d72 Mon Sep 17 00:00:00 2001 From: Pradeep Dhupar <148972812+PradeepITT@users.noreply.github.com> Date: Wed, 3 Apr 2024 16:32:25 +0530 Subject: [PATCH 3/3] Delete Try_Catch.cpp --- Try_Catch.cpp | 73 --------------------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 Try_Catch.cpp diff --git a/Try_Catch.cpp b/Try_Catch.cpp deleted file mode 100644 index 2fa6710..0000000 --- a/Try_Catch.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include - -using namespace std; - -class CarDamageException : public exception { -private: - string message; -public: - CarDamageException(const char* msg) : message(msg) {} - const char* what() const noexcept override { - return message.c_str(); - } -}; - -class BrakeFailureException : public exception { -private: - string message; -public: - BrakeFailureException(const char* msg) : message(msg) {} - const char* what() const noexcept override { - return message.c_str(); - } -}; - -class Car { -public: - void driveSafely(bool collision, bool brakeFailure) { - if (collision) { - throw CarDamageException("Car damaged due to collision."); - } else if (brakeFailure) { - throw BrakeFailureException("Brake Failure Occurred"); - } - } -}; - -class AccidentHandler { -public: - static void handleCarDamage(const char* message) { - cout << "Car damaged: " << message << ". Insurance claim process initiated." << endl; - } - - static void handleBrakeFailure(const char* message) { - cout << "Brake failure detected: " << message << ". Pull over safely and engage emergency brake." << endl; - } - - static void cleanUpAfterAccident() { - cout << "Clean-up operations after accident completed." << endl; - } -}; - -class CarAccident { -public: - static void main() { - Car car; - try { - car.driveSafely(false, true); - cout << "Both persons reached the destination and are safe." << endl; - } catch (CarDamageException& e) { - AccidentHandler::handleCarDamage(e.what()); - } catch (BrakeFailureException& e) { - AccidentHandler::handleBrakeFailure(e.what()); - } catch (...) { - cerr << "Unknown exception occurred." << endl; - } - AccidentHandler::cleanUpAfterAccident(); - } -}; - -int main() { - CarAccident::main(); - return 0; -}