Egyedi kivételkezelés 1.

Filed Under (kód) by nameless on 02-08-2009

Ez a forráskód úgy van megoldva, hogy több .cs fájl foglal magába. Project -> Add new Item -> Class
Egyedi kivételkezelés 1.

program.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace CustomException
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("***** Fun with Custom Exceptions *****\n");
      Car myCar = new Car("Rusty", 90);
      try
      {
        // Trip exception.
        myCar.Accelerate(50);
      }
      catch (CarIsDeadException e)
      {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.TimeStamp);
        Console.WriteLine(e.Cause);
      }
      Console.ReadLine();
    }
  }
}

cars osztály:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace CustomException
{
  public class Car
  {
    #region Member variables & Constructors
    // Constant for maximum speed.
    public const int MaxSpeed = 100;
 
    // Internal state data.
    private int currSpeed;
    private string petName;
 
    // Is the car still operational?
    private bool carIsDead;
 
    // A car has-a radio.
    private Radio theMusicBox = new Radio();
 
    // Constructors.
    public Car() {}
    public Car(string name, int currSp)
    {
      currSpeed = currSp;
      petName = name;
    }
    #endregion
 
    #region Methods
    public void CrankTunes(bool state)
    {
      // Delegate request to inner object.
      theMusicBox.TurnOn(state);
    }
 
    // This time, throw an exception if the user speeds up beyond MaxSpeed.
    public void Accelerate(int delta)
    {
      if (carIsDead)
        Console.WriteLine("{0} is out of order...", petName);
      else
      {
        currSpeed += delta;
        if (currSpeed >= MaxSpeed)
        {
          carIsDead = true;
          currSpeed = 0;
 
          // We need to call the HelpLink property, thus we need
          // to create a local variable before throwing the Exception object.
          CarIsDeadException ex =
          new CarIsDeadException(string.Format("{0} has overheated!", petName),
          "You have a lead foot", DateTime.Now);
          ex.HelpLink = "http://www.CarsRUs.com";
          throw ex;
        }
        else
          Console.WriteLine("=> CurrSpeed = {0}", currSpeed);
      }
    }
    #endregion
  }
}

rádió osztály:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace CustomException
{
  public class Radio
  {
    public void TurnOn(bool on)
    {
      if(on)
        Console.WriteLine("Jamming...");
      else
        Console.WriteLine("Quiet time...");
    }
  }
}
 
CarIsDead:
 
public class CarIsDeadException : ApplicationException
{
    private string messageDetails;
    private DateTime errorTimeStamp;
    private string causeOfError;
 
    public DateTime TimeStamp
    {
         get { return errorTimeStamp; }
         set { errorTimeStamp = value; }
    }
    public string Cause
    {
        get { return causeOfError; }
        set { causeOfError = value; }
    }
 
    public CarIsDeadException() { }
    public CarIsDeadException(string message,
    string cause, DateTime time)
    {
       messageDetails = message;
       causeOfError = cause;
      errorTimeStamp = time;
    }
    // Override the Exception.Message property.
    public override string Message
    {
       get
       {
           return string.Format("Car Error Message: {0}", messageDetails);
       }
    }
}

Post a comment