// ExceptionStack.cs // // ************************ // (c) 2001 Luke Venediger // // For Comments, Feedback and good South African Beer // please contact me on the address below: // lukev123@hotmail.com // ************************ // // This set of classes shows you how to do the following: // 1. Make use of the try-catch Exception stack, which allows // you to trap problems that are specific, to those // that are unspecific and erratic // 2. Create your own exception classes that can be used // to trap possible unstable situations in libraries // that you have developed. // Use some base classes using System; // This is our first class: A Vehicle Exception class VehicleException : Exception { // Constructor, here is where we set our Exception-Specific properties public VehicleException(string errorMessage) : base(errorMessage) { // Extra trace code added here... } } // This is our second(child) class: A MotorCar Exception, which // inherits from VehicleException class MotorCarException : VehicleException { // Constructor public MotorCarException(string errorMessage) : base(errorMessage) { // Extra trace code added here... } } // This is our third(child) class: A Ferrari Exception, which // inherits from MotorCar exception class FerrariException : MotorCarException { // Constructor public FerrariException(string errorMessage) : base(errorMessage) { // Extra trace code added here... } } // Our driver class, which will make use of all the // Exceptions listed above public class CheckCar { // Purpose: To check the Ferrari Badge on the front public void checkFerrariBadge() { // We will purposefully throw an exception here, // because our careful inspection has shown us // that someone has painted the badge orange! throw new FerrariException("Your Ferrari Badge has been painted orange!"); } // Purpose: To check motor oil level public void checkMotorOil() { // Here another exception is thrown because // we notice that the car needs a few cans // of oil for the engine. throw new MotorCarException("Your motor car is very low on oil."); } // Purpose: To check that the vehicle is able to move public void checkVehicleMobility() { // Oh dear, this vehicle does not want to // become mobile at all, so this raises our // exception. throw new VehicleException("Your vehicle is not mobile."); } // *** // The Main class, which will display all of our // Exceptions, as we catch them. Note that there is a stack which // each exception will trickle down through, until it // hits the best-matching error condition (exception). public static void Main() { // Create us an object CheckCar checkingCar = new CheckCar(); Console.WriteLine("\nChecking your car with the exception stack in the order:"); Console.WriteLine("FerrariException->MotorCarException->VehicleException\n\n"); // Here we place the methods to be called in a try-catch // block in order to trap any exception conditions that // could arise. try { // Please un-comment the appropriate line: // checkingCar.checkVehicleMobility(); checkingCar.checkMotorOil(); // checkingCar.checkFerrariBadge(); } catch (FerrariException fe) { Console.WriteLine("FerrariException: " + fe.ToString()); } catch (MotorCarException mce) { Console.WriteLine("MotorCarException: " + mce.ToString()); } catch (VehicleException ve) { Console.WriteLine("VehicleException: " + ve.ToString()); } // The commented block below attempts to set up an exception // stack where the super-exception is checked before the child-exceptions // are checked. This causes (thankfully) a compiler error. Uncomment // the code and see what happens for yourself. /* Console.WriteLine("\n\n\nChecking your car with the exception stack in the order:"); Console.WriteLine("VehicleException->MotorCarException->FerrariException\n\n"); try { // Please un-comment the appropriate line: checkingCar.checkVehicleMobility(); checkingCar.checkMotorOil(); checkingCar.checkFerrariBadge(); } catch (VehicleException ve) { Console.WriteLine("VehicleException: " + ve.ToString()); } catch (MotorCarException mce) { Console.WriteLine("MotorCarException: " + mce.ToString()); } catch (FerrariException fe) { Console.WriteLine("FerrariException: " + fe.ToString()); } */ } }