C# Exception handling
An exception is any error that occur during the execution of the program. The C# exception handling mechanism helps you to detect and handle these run-time errors.
In C#, exception handling is done with the help of the following four keywords:
try – The try block contains the statements that might create an exception. It is followed by one or more catch blocks.
catch – The catch block handles the exceptions thrown from try block.
finally – Code specified in finally block is always executed. It can be used to release resources used in the try block. For example, closing any database connections or files opened in the try block.
throw – Exceptions can be created using the throw keyword.
using System; //Adding namespaces
namespace LearnCSharp //Namespace of the class
class Program //The class
static void Main(string args) //Main method
int a = 10;
int b = 0;
int c = 0;
c = a / b;
Console.WriteLine("Division by zero is not possible");
//Printing error message
Division by zero is not possible
Attempted to divide by zero.