Throwing a Generic Exception
Now that we have a functional Car type, I’ll illustrate the simplest way to throw an exception. The
current implementation of Accelerate() simply displays an error message if the caller attempts to
speed up the Car beyond its upper limit.
To retrofit this method to throw an exception if the user attempts to speed up the automobile
after it has met its maker, you want to create and configure a new instance of the System.Exception
class, setting the value of the read-only Message property via the class constructor. When you wish to
send the error object back to the caller, make use of the C# throw keyword. Here is the relevant code
update to the Accelerate() method:
Do'stlaringiz bilan baham: |