C# program to catch all the exceptions using Exception class
Program.cs
int a = 1, b = 0;
try
{
    try
    {
        Console.WriteLine(a / b);
    }
    catch (Exception e)
    {
        Console.WriteLine("First Exception: " + e.ToString());
    }
    string s = null;
    Console.WriteLine(s.Length);

}
catch (Exception e)
{
    Console.WriteLine("Second Exception: " + e.ToString());
}
Output
kodingwindow@kw:~/csharp$ dotnet run
First Exception: System.DivideByZeroException: Attempted to divide by zero.
    at Program.<Main>$(String[] args) in /home/kodingwindow/csharp/Program.cs:line 6
 Second Exception: System.NullReferenceException: Object reference not set to an instance of an object.
    at Program.<Main>$(String[] args) in /home/kodingwindow/csharp/Program.cs:line 13 
kodingwindow@kw:~/csharp$ 
Advertisement