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