kodingwindow@kw:~/csharp$ dotnet run
Unhandled 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 2
kodingwindow@kw:~/csharp$
C# program to demonstrate the use of try and catch blocks
Program.cs
try{strings=null;Console.WriteLine(s.Length);}catch(NullReferenceExceptione){Console.WriteLine("String is null, hence, unable to find the length.");}
Output
kodingwindow@kw:~/csharp$ dotnet run
String is null, hence, unable to find the length.
kodingwindow@kw:~/csharp$
C# program to demonstrate the use of try with multiple catch blocks
Program.cs
try{strings=null;Console.WriteLine(s.Length);}catch(InvalidOperationExceptione){Console.WriteLine("Exception caught in the first catch block");}catch(NullReferenceExceptione){Console.WriteLine("Exception caught in the second catch block");}
Output
kodingwindow@kw:~/csharp$ dotnet run
Exception caught in the second 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.