Exception Handeling in C#


// It is for runtime errors
// Exceptions abnormally terminate the program flow
// The action to be performed in case of occurance of an exception is not known to the program so we need to give it an exception handeler code
// We can have multiple catch blocks for one try block to catch different kinds of exceptions

try
{
  int[] myNumbers = {1, 2, 3};
  Console.WriteLine(myNumbers[10]);
}
catch (SomeSpecificException e)
{
  Console.WriteLine("Something went wrong in try block.");
}
finally
{
  Console.WriteLine("This runs always, exception or not!");
}
throw keyword is used to manually throw exceptions
throw new Exception("msg")

Comments