codeaft@codeaft:~/csharp$ dotnet run
First Exception: System.DivideByZeroException: Attempted to divide by zero.
at Program.<Main>$(String[] args) in /home/codeaft/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/codeaft/csharp/Program.cs:line 13
codeaft@codeaft:~/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
codeaft@codeaft:~/csharp$ dotnet run
Exception caught in the first nested catch block
Exception caught in the third catch block
codeaft@codeaft:~/csharp$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.