Following the example in book and got the following slightly modified code:
class OutOfHoneyException : Exception
{
public OutOfHoneyException(string message) :base(message){}
}
class HoneyDeliverSystem
{
public void FeedHoneyToEggs()
{
if (true)
{
throw new OutOfHoneyException("This hive is Out Of Honey");
}
}
}
.....................................
HoneyDeliverSystem delivery = new HoneyDeliverSystem();
try
{
delivery.FeedHoneyToEggs();
}
catch (OutOfHoneyException ex)
{
Console.WriteLine(ex.Message);
}
What I understand when ever a specific exception is thrown by us in specific condition, the corresponding catch block handles that.
But please help me with a better example, maybe .NET exception's implementation will be really helpful.
And why are we passing the message to the base Exception
class? Is it only for printing purpose?
There is a OOPS concept for child class calling base class constructor. Could you please name it and how it is related to custom exceptions example?