Exercise: Voter Registration Validator
Problem statement
You are building a voter registration utility for a local municipality. The system receives applicant ages from a web form. Due to occasional data entry errors, some ages arrive as negative numbers. Your utility must detect these invalid records, reject them by explicitly throwing an exception, and handle the error gracefully so the system does not crash while continuing to log the completion of the verification attempt.
Task requirements
Complete the
VerifyVoterEligibilitymethod to check the applicant’s age.If the age is less than
0, generate an error with the exact message"Age cannot be negative."In the main execution area, call this method inside a protected block.
If the method generates an error, intercept it and print the error message to the console.
Ensure that the phrase
"Verification process finished."is printed to the console regardless of whether an error occurred or the validation succeeded.
Constraints
Use the
throwkeyword to raise a built-inArgumentExceptionwhen the age is negative.Use a
try...catch...finallystructure to handle the exception and ensure the final completion message always prints.Do not create custom exception classes; rely on the standard .NET exceptions.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use the
throw newsyntax followed by the exception type to manually trigger the error inside your validation method.Your
catchblock needs to specifically catchArgumentExceptionand assign it to a variable (likeex) so you can accessex.Message.The
finallyblock is the perfect place to put your completion message, as it guarantees execution even after acatchblock resolves an error.
Exercise: Voter Registration Validator
Problem statement
You are building a voter registration utility for a local municipality. The system receives applicant ages from a web form. Due to occasional data entry errors, some ages arrive as negative numbers. Your utility must detect these invalid records, reject them by explicitly throwing an exception, and handle the error gracefully so the system does not crash while continuing to log the completion of the verification attempt.
Task requirements
Complete the
VerifyVoterEligibilitymethod to check the applicant’s age.If the age is less than
0, generate an error with the exact message"Age cannot be negative."In the main execution area, call this method inside a protected block.
If the method generates an error, intercept it and print the error message to the console.
Ensure that the phrase
"Verification process finished."is printed to the console regardless of whether an error occurred or the validation succeeded.
Constraints
Use the
throwkeyword to raise a built-inArgumentExceptionwhen the age is negative.Use a
try...catch...finallystructure to handle the exception and ensure the final completion message always prints.Do not create custom exception classes; rely on the standard .NET exceptions.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Use the
throw newsyntax followed by the exception type to manually trigger the error inside your validation method.Your
catchblock needs to specifically catchArgumentExceptionand assign it to a variable (likeex) so you can accessex.Message.The
finallyblock is the perfect place to put your completion message, as it guarantees execution even after acatchblock resolves an error.