Advertisement
elena1234

How to make exception

Apr 14th, 2021 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. // in folder Exceptions write your Exceptions messages
  2. namespace Vehicles.Exceptions
  3. {
  4.     public static class ExceptionMessages
  5.     {
  6.         public const string NotEnoughFuelMessage = "{0} needs refueling"; // without $
  7.  
  8.         public const string InvalidVehicleTypeMessage = "Type is not valid";
  9.     }
  10. }
  11.  
  12.  
  13. // in folder Models or in folder Factory you throw the exception
  14. //  string exceptionMessage = String.Format(ExceptionMessages.NotEnoughFuelMessage, this.GetType().Name);
  15. //  throw new InvalidOperationException(exceptionMessage);
  16. // or  throw new ArgumentException(String.Format(ExceptionMessages.InvalidVehicleTypeMessage));
  17.  
  18.  
  19. // in Engine you catch the exception
  20.        try
  21.          {
  22.          }
  23.        catch (InvalidOperationException ex)
  24.          {
  25.               Console.WriteLine(ex.Message); // "The Fuel is not enough" -  only this one
  26.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement