Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions BrakeFailureException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>
#include <stdexcept>
using namespace std;

class BrakeFailureException : public exception {
public:
BrakeFailureException(const char* message) : exception(message) {}
};

46 changes: 46 additions & 0 deletions CarAccident.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <stdexcept>

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to figure out in which condition this code will process PersonInjuredException exception

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;
}
};
9 changes: 9 additions & 0 deletions CarDamageException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>
#include <stdexcept>

using namespace std;

class CarDamageException : public exception {
public:
CarDamageException(const char* message) : exception(message) {}
};
9 changes: 9 additions & 0 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>
#include <stdexcept>

using namespace std;

int main() {
CarAccident::main();
return 0;
}
8 changes: 8 additions & 0 deletions PersonInjuredException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include <stdexcept>
using namespace std;

class PersonInjuredException : public exception {
public:
PersonInjuredException(const char* message) : exception(message) {}
};