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$
C# program to demonstrate the use of nested try and catch blocks
Program.cs
strings=null;intn=515,d=0,result=0;try{Console.WriteLine("Length: "+s.Length);}catch(InvalidOperationExceptione){Console.WriteLine("Exception caught in the first catch block");}catch(Exceptione1){try{result=n/d;}catch(DivideByZeroExceptione2){Console.WriteLine("Exception caught in the first nested catch block");}Console.WriteLine("Exception caught in the third catch block");}
Output
kodingwindow@kw:~/csharp$ dotnet run
Exception caught in the first nested catch block
Exception caught in the third catch block
kodingwindow@kw:~/csharp$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.