exceptionssystemexception class.md - brainchildservices/curriculum GitHub Wiki

Slide 1

Exception Classes in C#

The exception classes in C# are mainly directly or indirectly derived from the System.Exception class.
Some of the exception classes derived from the System.Exception class are the System.ApplicationException and System.SystemException classes.

  • The System.ApplicationException class supports exceptions generated by application programs. Hence the exceptions defined by the programmers should derive from this class.
  • The System.SystemException class is the base class for all predefined system exception.

Slide 2

Unhandled Exception:

If our program is not prepared to handle any kind of exception that it might run into, it would lead to the program crashing and prevent running of the rest of the program.
We have a method that returns value of a number at the array index if we give the position of the index as parameter.

  • The program works fine executing all the statements in the Main method if we pass a value that is less than or equal to the index of the array.

  • The program crashes if we send an invalid position. And the statements after the method call are not executed.

    using System;
    
    public class Program
    {
    	public static void Main()
    	{
    		Console.WriteLine("Going to call GetNumber method");
    		int result = GetNumber(11);
      
    		Console.WriteLine("Method call completed");
    		Console.WriteLine("The value retured from the method is: " + result);
    		Console.WriteLine("Program control in Main method");
    	}
    
    	public static int GetNumber(int position)
    	{
    		int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
    		return arr[position];
    	}
    }
    

Slide 3

Case1: Try-Catch to make the program work

We used a try - catch block to make the program work. If any exception occurs in the try block, the control will move to the catch block and then the statements after the catch block.
However, the result that we get is not correct and we are not aware of what caused the exception any longer.
try-catch shouldn't be used to just skip the exceptions but to make sure that we are aware of the cause that leads to the exception.

Slide 4

Case2: Try-Catch & exception message, exception Stack trace

In this case we pass the whatever exception was caught to ex. Using this we can get several more information about the exception.
However the result we get is still not correct.

 using System;

 public class Program
 {
 	public static void Main()
 	{
 		Console.WriteLine("Going to call GetNumber method");
 		int result = GetNumber(11);
 		Console.WriteLine("Method call completed");
 		Console.WriteLine("The value retured from the method is: " + result);
 		Console.WriteLine("Program control in Main method");
 	}

 	public static int GetNumber(int position)
 	{
 		int output = 0;
 		try
 		{
 			int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
 			output = arr[position];
 		}
 		catch (Exception ex) 					//The catch is going to call every exception and put into variable ex
 		{
 			Console.WriteLine(ex.Message);		//exhas several exception methods that can be used to study the details of the exception
 			Console.WriteLine(ex.StackTrace);
 		}
 
 		return output;
 	}
 }

Slide 4

Case3: Using the tr-catch block at the point of code where the exception could start in the stack trace

 using System;

 public class Program
 {
 	public static void Main()
 	{
 		Console.WriteLine("Going to call GetNumber method");
 		try
 		{
 			int result = GetNumber(1);
 			Console.WriteLine("Method call completed");
 			Console.WriteLine("The value retured from the method is: " + result);
 		}
 		catch (Exception ex) 					//The catch is going to call every exception and put into variable ex
 		{
 			Console.WriteLine(ex.Message); 		//exhas several exception methods that can be used to study the details of the exception
 			Console.WriteLine(ex.StackTrace);
 		}

 		Console.WriteLine("Program control in Main method");
 	}

 	public static int GetNumber(int position)
 	{
 		int output = 0;
 		int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
 		output = arr[position];
 		return output;
 	}
 }