Exception Handling
- Andre Johnson
- Aug 28, 2023
- 1 min read
One class that incorporates exception handling. The Animal class will have the following characteristics:
Animal
- name : string
- dateOfBirth : DateTime
- status : string
+ Animal(string name, string dob, string status)
+ getName : string
<<property>> + status : string
+ override ToString() : string
The Animal constructor implements Exception Handling to ensure that no instance is created without a valid date of birth. Provide the user an opportunity to correct the error before proceeding.
A Drive program was created to initialize an object of class Animal, provide an invalid value for the date of birth, demonstrate the Exception handling, and utilize a printData function to print a formatted string of the data values within the object.
***
Hi Andre,
Very well done!
You properly implemented the Try/Catch blocks for the FormatException regarding converting the string to a date.
Another option (not that there is anything wrong with the way you did this) would have been to use the DateTime.Parse() method, which automatically throws the exception, and then you can customize the output in the catch block.
Well done.
***

Comments