Hi Guys
NP82 catch() method
Program is coming under Exception Handling. Incomplete program is given. Students have to develop up the balance. Clue is given. One of the clue is catch(CarIsDeadException e). When executing completed program which is producing error result.
When I changed CarIsDeadException into Exception. That means catch(CarIsDeadException e) is now catch(Exception e). After the change program is executing well.
I wish to know what should come in catch() method whether it is CarIsDeadException or Exception. Is it a printing mistake?
Anyone knows please explain the reason.
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;
}
// This custom exception describes the details of the car-is-dead condition.
public class CarIsDeadException : System.Exception
{
// This custom exception maintains the name of the doomed car.
private string carName;
public CarIsDeadException() { }
public CarIsDeadException(string carName)
{
this.carName = carName;
}
// Override the Exception.Message property.
public override string Message
{
get
{
string msg = base.Message;
if (carName != null)
msg += carName + " has bought the farm...";
return msg;
}
}
}
// Throw the custom CarIsDeadException.
public void SpeedUp(int delta)
{
// If the car is dead, just say so...
if(carIsDead)
{
// Throw 'car is dead' exception.
throw new CarIsDeadException(this.petName);
}
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
{
// 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 (CarIsDeadException e)
{
Console.WriteLine("\nMethod: {0}", e.TargetSite);
Console.WriteLine("\nMessage: {0}", e.Message);
}
// The error has been handled, continue on with the flow of this app...
Console.WriteLine("\n***** Out of exception logic *****");
return 0;
}
}
}
Posted Feb 17, 2008, 5:51 AM
Thank you for your explanation, Alan.
AlanPosted Feb 16, 2008, 6:58 PM
The Car.CarIsDeadException class is derived from the Exception class and therefore inherits its TargetSite property.
It also overrides the Exception class's Message property, which is virtual, with its own message.
So, if you catch an Exception rather than a Car.CarIsDeadException, it will be the runtime type of the exception (namely Car.CarIsDeadException) which will determine which version of the Message property gets printed out.
Also the same value will be printed out for TargetSite whichever exception type is caught.
In other words you'll get the same output in both cases.
Posted Feb 16, 2008, 2:33 PM
Both catch (Car.CarIsDeadException e) and catch(Exception e) are producing same output. Please explain the reason for that.
AlanPosted Feb 16, 2008, 2:16 PM
There is in fact a mistake in the program, Maha.
The CarIsDeadException class is nested inside the Car class. So to access it from the CarApp class you need to qualify it with the name of the outer class.
So, if you change this line:
catch (CarIsDeadException e)
to this:
catch (Car.CarIsDeadException e)
then it should work.