© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
79 replies
Faker

✅ Error handling in C#

Hello guys, consider the following code:

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");
            }
        }
    }
}
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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Exception Handling in C#
C#CC# / help
13mo ago
Error handling through SQL or C#.
C#CC# / help
14mo ago
Handling reading large files in C#
C#CC# / help
2y ago
Uncaught Exception C++ hostfxr Handling
C#CC# / help
4y ago