Exercise: Voter Registration Validator

Validate voter ages and safely handle invalid data using structured exception handling.

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 VerifyVoterEligibility method 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 throw keyword to raise a built-in ArgumentException when the age is negative.

  • Use a try...catch...finally structure 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 new syntax followed by the exception type to manually trigger the error inside your validation method.

  • Your catch block needs to specifically catch ArgumentException and assign it to a variable (like ex) so you can access ex.Message.

  • The finally block is the perfect place to put your completion message, as it guarantees execution even after a catch block resolves an error.

Exercise: Voter Registration Validator

Validate voter ages and safely handle invalid data using structured exception handling.

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 VerifyVoterEligibility method 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 throw keyword to raise a built-in ArgumentException when the age is negative.

  • Use a try...catch...finally structure 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 new syntax followed by the exception type to manually trigger the error inside your validation method.

  • Your catch block needs to specifically catch ArgumentException and assign it to a variable (like ex) so you can access ex.Message.

  • The finally block is the perfect place to put your completion message, as it guarantees execution even after a catch block resolves an error.

C# 14.0
int applicantAge = -5;
// TODO: Wrap the method call in a try-catch-finally block.
// Inside the try block, call VerifyVoterEligibility(applicantAge).
// Catch an ArgumentException and print its Message property.
// Use finally to print "Verification process finished."
// --- Helper Method ---
void VerifyVoterEligibility(int age)
{
// TODO: If age is less than 0, throw an ArgumentException with the message: "Age cannot be negative."
if (age >= 18)
{
Console.WriteLine("Eligible to vote.");
}
else
{
Console.WriteLine("Not eligible to vote.");
}
}