Can we write try block without Catch Block in C#.net


In the previous example we have learnt how to handle exceptions at constructor level in C#.net. Now i am going to explain can we use try block with out catch block in CSharp.Net.

Explanation: We can use try block with out catch block by declaring finally block which is syntactically correct in C#.net. This is one of the best c#.net Interview Question

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int i = 0, j = 1, result;
                Console.WriteLine("this is try block");

            }
            //catch(Exception ex)
            //{
              //  Console.WriteLine(ex.Message);
                //Console.WriteLine("this is Catch block");
            //}
            finally
            {
                Console.WriteLine("this is finally block");
                Console.Read();
            }

        }
    }
}

Output:


If you have any queries or suggestions, please feel free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar