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
59 changes: 59 additions & 0 deletions CarAccidentErrorHandling/CarAccidentErrorHandling/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace CarAccidentErrorHandling
{
public class Car
{
public void Start()
{
Console.WriteLine("Car started.");
}

public void Run()
{
Console.WriteLine("Car is running.");
DrivingScenario scenario = (DrivingScenario)new Random().Next(1, 6); // Simulate random car failure/accident scenarios
switch (scenario)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can it be done without switch case as it breaks OCP.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I have used switch case just so I can simulate different accident scenarios randomly. In actual case, we would have a method inside the Run method which will throw any of the exceptions that we'll get.
Let me know what you think about it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then it is ok else everything looks good to me.

{
case DrivingScenario.Collision:
throw new CollisionException("Car collided with another vehicle!");
case DrivingScenario.RoadObstacle:
throw new RoadObstacleException("Car encountered a road obstacle!");
case DrivingScenario.PedestrianCollision:
throw new PedestrianCollisionException("Car hit a pedestrian!");
case DrivingScenario.MechanicalFailure:
throw new MechanicalFailureException("Car experienced a mechanical failure!");
case DrivingScenario.Safe:
Console.WriteLine("Car reached its destination safely.");
break;
}
}

public void Stop()
{
Console.WriteLine("Car stopped.");
}

public void Brake()
{
Console.WriteLine("Braking to avoid accident.");
}

public void Swerve()
{
Console.WriteLine("Swerving to avoid obstacle.");
}

public void ResetCarState()
{
Console.WriteLine("Reseting car's state like turn off lights, any indicator or alarms, set gear to neutral, etc.")
}

public enum DrivingScenario
{
Collision = 1,
RoadObstacle = 2,
PedestrianCollision = 3,
MechanicalFailure = 4,
Safe = 5
}
}
}
45 changes: 45 additions & 0 deletions CarAccidentErrorHandling/CarAccidentErrorHandling/CarOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace CarAccidentErrorHandling
{
internal class CarOperator
{
static void Main(string[] args)
{
Car car = new Car();
try
{
car.Start();
car.Run();
}
catch (CollisionException collisionException)
{
Console.WriteLine("Car Accident: " + collisionException.Message);
car.Brake();
}
catch (RoadObstacleException roadObstaclException)
{
Console.WriteLine("Road Obstacle: " + roadObstaclException.Message);
car.Swerve();
car.Brake();
}
catch (PedestrianCollisionException pedestrianException)
{
Console.WriteLine("Pedestrian Accident: " + pedestrianException.Message);
car.Brake();
}
catch (MechanicalFailureException mechanicalFailureException)
{
Console.WriteLine("Mechanical Failure: " + mechanicalFailureException.Message);
}
catch (Exception exception)
{
Console.WriteLine("An unexpected error occurred: " + exception.Message);
}
finally
{
Console.WriteLine("Attempting to bring the car back to a normal state");
car.Stop();
car.ResetCarState();
}
}
}
}
22 changes: 22 additions & 0 deletions CarAccidentErrorHandling/CarAccidentErrorHandling/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace CarAccidentErrorHandling
{
public class CollisionException : Exception
{
public CollisionException(string message) : base(message) { }
}

public class RoadObstacleException : Exception
{
public RoadObstacleException(string message) : base(message) { }
}

public class PedestrianCollisionException : Exception
{
public PedestrianCollisionException(string message) : base(message) { }
}

public class MechanicalFailureException : Exception
{
public MechanicalFailureException(string message) : base(message) { }
}
}
6 changes: 6 additions & 0 deletions CarAccidentErrorHandling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Assignment - Error Handling in Car Accident Scenario

![Car accident image on basis of which we have to think of try-catch scenarios.](https://www.oreilly.com/api/v2/epubs/9780136083238/files/images/103fig01.jpg)

- You have to think about given image and come with scenarios.
- What are the try and catches are occurring into that image. You have to find different kind of exceptions. You have to write them in code with proper sequence.