Search⌘ K
AI Features

Solution: Voter Registration Validator

Understand how to implement exception handling in C# by validating voter registration input. Explore the use of try, catch, and finally blocks to manage errors and maintain program flow during validation checks.

We'll cover the following...
C# 14.0
int applicantAge = -5;
try
{
VerifyVoterEligibility(applicantAge);
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Verification process finished.");
}
// --- Helper Method ---
void VerifyVoterEligibility(int age)
{
if (age < 0)
{
throw new ArgumentException("Age cannot be negative.");
}
if (age >= 18)
{
Console.WriteLine("Eligible to vote.");
}
else
{
Console.WriteLine("Not eligible to vote.");
}
}
...