Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Checkpoint โ€” Error handling

โญ beginnerโณ 20 min read๐Ÿ“ Lesson 53 of 85

A reservation service: layered validation, a custom exception with data, and a cleanup-guaranteed executor.

Checkpoint: the reservation service

Task: implement in Solution:

  1. class BookingConflictException : Exception with public string Slot { get; } (constructor (string slot, string message) โ†’ base(message), store Slot).
  2. static string Book(List<string> takenSlots, string slot):
    • null/whitespace slot โ†’ ArgumentException;
    • slot already in takenSlots โ†’ BookingConflictException (Slot = slot);
    • otherwise add and return the slot.
  3. static T Guard<T>(Func<T> action, string label):
    • runs action; returns its result on success;
    • on exception: throws InvalidOperationException with a message containing label โ€” but ONLY for InvalidOperationException; any other exception type must propagate unchanged;
    • label null โ†’ ArgumentException before running action.