Lesson 8: Error and Exception Handling

An exception is an unforeseen problem that arises when you execute a program. Most of the time, your program should check for these problems, and handle them transparently and in a user friendly way. Infact, you should always build in ways to check for problems in your code, by verifying input, validating data, checking for nulls, etc. However, there are times where there are stuff that you can’t predict, like out of memory errors, file errors, or database errors. This is where exception handling comes into place.

When an exception happens, it is called ‘thrown’. So when and exception happens, and exception is thrown.
Here are some common exceptions:

Exception Class Description
System.IO.IOException Handles I/O errors.
System.IndexOutOfRangeException Handles errors generated when a method refers to an array index out of range.
System.ArrayTypeMismatchException Handles errors generated when type is mismatched with the array type.
System.NullReferenceException Handles errors generated from deferencing a null object.
System.DivideByZeroException Handles errors generated from dividing a dividend with zero.
System.InvalidCastException Handles errors generated during typecasting.
System.OutOfMemoryException Handles errors generated from insufficient free memory.
System.StackOverflowException Handles errors generated from stack overflow.

 

Most exceptions are thrown automatically by the program, however, you can throw you own exceptions.

To deal with exceptions, you use a Try – Catch statement. The try catch statement looks like this:

try
{
   // statements causing exception
}
catch( ExceptionName e1 )
{
   // error handling code
}
finally
{
   // statements to be executed
}

Here is an explanation of the Try-Catch statement:

  • try: A try block identifies a block of code for which particular exceptions will be activated. It’s followed by one or more catch blocks.
  • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
  • finally: The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown.

Here is an example, where we divide by zero.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson8_Errors
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
            division(65, 0);
            }
            catch( System.DivideByZeroException e)
            {
                Console.WriteLine(e);
                Console.WriteLine();
                Console.WriteLine("You tried to divide by 0! SKRUB!");
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
                Environment.Exit(0);

            }
            finally
            {
                Console.WriteLine("you finished you pleb");
            }
            Console.ReadKey();
        }
        public static void division(int yolo, int swag)
        {

            int division = yolo / swag;
            Console.WriteLine(division);
            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *