C#C
C#11mo ago
Faker

✅ Error handling in C#

Hello guys, consider the following code:

C#
using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person("john", 21, "john@gmail.com");
            Console.WriteLine(person);
            
            // Updating and veryfying name
            person.NameVerification("Somebody123");
            
            // Updating and verifying age
            person.AgeVerification(200);
            
            // Updating and verifying email
            var emailResult = person.IsValidEmail("johngmail.com");
            if (emailResult)
            {
                Console.WriteLine("Valid Email");
            }
            else
            {
                Console.WriteLine("Invalid Email");
            }
        }
    }
}


If I try to wrap the AgeVerification in a try catch block and pass a string instead of an int, my IDE yell an error at me, like its not a runtime exception (I think we say it's a checked exception), why is that? Even with the try catch. In my class Person, I didn't include a try catch in the method doing the work though.

Normally, should we also include try catch block in the Person class? Like for AgeVerification here?
Was this page helpful?