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 200 views
1 answer 420 views
2 answers 301 views
1 answer 290 views
1 answer 240 views
240 views asked Nov 23, 2020 by avibootz
...