Learning to create and throw Exceptions.
- Run the
main
method in theDriveTheCar
class. - Look at the output on the terminal
- Familiarize yourself with the
Car
andDriveTheCar
class.
Try this:
-
Create a
CarNotStartedException
Exception that is thrown by theCar
class when theCar
is driven without being started.- Change your
drive
method to throw aCarNotStartedException
.
- Change your
-
Create a
CarOutOfFuelException
Exception that is thrown by theCar
class when it runs out of fuel.- Change your
drive
method to throw aCarOutOfFuelException
.
- Change your
-
Create a
CarException
Exception and make it the super class of bothCarNotStartedException
andCarOutOfFuelException
.- Change your
drive
method to throw aCarException
. - How does that effect your code?
- Change your
Note: you will need to handle the exceptions in the main method of the DriveTheCarClass
.
Make sure that the same messages are still printed to the console as before.