Hi Guys
NP81 Exception
Following program is showing an execuation of a SimpleException type.
Console.WriteLine("Member type: {0}", e.TargetSite.MemberType); is giving Method output what is meant by Method.
Console.WriteLine("Member name: {0}", e.TargetSite); is giving Void SpeedUp(Int32) output what is meant by Void.
Anyone knows please explain.
Thank you
using System;
namespace SimpleException
{
// C# allows nesting of classes, interfaces and structures.
// Here, the car nests the radio.
public class Car
// Internal state data.
private int currSpeed;
private int maxSpeed;
private string petName;
bool carIsDead; // Is the car alive or dead?
public Car(string name, int max, int curr)
currSpeed = curr;
maxSpeed = max;
petName = name;
carIsDead = false;
}
public void SpeedUp(int delta)
// If the car is dead, just say so...
if (carIsDead)
throw new Exception("This car is already dead...");
else // Not dead, speed up.
currSpeed += delta;
if (currSpeed >= maxSpeed)
//Console.WriteLine("Sorry, {0} has overheated...", petName);
carIsDead = true;
else
Console.WriteLine("=> CurrSpeed = {0}", currSpeed);
public class CarApp
// Speed up the car safely...
public static int Main(string[] args)
// Make a car.
Car buddha = new Car("Buddha", 100, 20);
// Speed up past the car's max speed to
// trigger the exception.
try
for (int i = 0; i < 10; i++)
buddha.SpeedUp(10);
catch (Exception e)
Console.WriteLine("\n*** Error! ***");
Console.WriteLine("Class defining member: {0}", e.TargetSite.DeclaringType);
SimpleException.Car
Console.WriteLine("Member type: {0}", e.TargetSite.MemberType);
Method
Console.WriteLine("Member name: {0}", e.TargetSite);
Void SpeedUp(Int32)
Console.WriteLine("Message: {0}", e.Message);
This car is already dead...
Console.WriteLine("Source: {0}", e.Source);
Source: SimpleException
// The error has been handled, continue on with the flow of this app...
Console.WriteLine("\n***** Out of exception logic *****");
return 0;
/*
=> CurrSpeed = 30
=> CurrSpeed = 40
=> CurrSpeed = 50
=> CurrSpeed = 60
=> CurrSpeed = 70
=> CurrSpeed = 80
=> CurrSpeed = 90
*** Error! ***
Class defining member: SimpleException.Car
Member type: Method
Member name: Void SpeedUp(Int32)
Message: This car is already dead...
***** Out of exception logic *****
*/