How to use try with multiple catch and finally for exception handling in C#

1 Answer

0 votes
using System;   
using System.IO;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string s;

            try
            {
                s = File.ReadAllText("d:\\blabla.txt");
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("finally");
            }
        }
    }
}


/*
run:
    
Could not find file 'd:\blabla.txt'.
finally

*/

 



answered Apr 26, 2017 by avibootz

Related questions

1 answer 197 views
1 answer 416 views
2 answers 298 views
1 answer 285 views
1 answer 235 views
235 views asked Nov 23, 2020 by avibootz
...