C# Exceptions

When using C # code, various errors may occur: editorial errors made by the editor, errors due to incorrect input, or other unexpected items.

In the event of an error, C # will usually stop and produce an error message. The technical term for this is: C# will issue an exception (throw an error).


C# try and catch

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

The try and catch keywords come in pairs:

Syntax
try 
            {
              //  Block of code to try
            }
            catch (Exception e)
            {
              //  Block of code to handle errors
            }

Consider the following example, in which we compiled a list of three whole numbers:

This will generate an error, because myNumbers[10] does not exist.

int[] myNumbers = {1, 2, 3};
            Console.WriteLine(myNumbers[10]); // error!

In the event of an error, we can use a try ... catch to catch the error and issue a specific code to handle it

In the following example, we use variations within the capture block (e) and the built-in Message structure, which produces a message explaining the difference:

Example
try
            {
              int[] myNumbers = {1, 2, 3};
              Console.WriteLine(myNumbers[10]);
            }
            catch (Exception e)
            {
              Console.WriteLine(e.Message);
            }

You can also extract your error message:

Example
try
            {
              int[] myNumbers = {1, 2, 3};
              Console.WriteLine(myNumbers[10]);
            }
            catch (Exception e)
            {
              Console.WriteLine("Something went wrong.");
            }

Finally

The finally statement allows you to create code, after try...catch, regardless of the result:

Example
try
            {
              int[] myNumbers = {1, 2, 3};
              Console.WriteLine(myNumbers[10]);
            }
            catch (Exception e)
            {
              Console.WriteLine("Something went wrong.");
            }
            finally
            {
              Console.WriteLine("The 'try catch' is finished.");
            }
            

The throw keyword

Throw Statement allows you to create a custom error.

The throw statement is used with a separate section. There are many different classes available in C #: ArithmeticException, FileNotFoundException, IndexOutOfRangeException, TimeOutException, etc:

Example
static void checkAge(int age)
            {
              if (age < 18)
              {
                throw new ArithmeticException("Access denied - You must be at least 18 years old.");
              }
              else
              {
                Console.WriteLine("Access granted - You are old enough!");
              }
            }
            
            static void Main(string[] args)
            {
              checkAge(15);
            }

If age was 20 years old, you would not get an exception:

Example
checkAge(20);