How to use try with multiple catch 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 (UnauthorizedAccessException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (PathTooLongException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


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

*/

 



answered Apr 25, 2017 by avibootz

Related questions

1 answer 419 views
1 answer 290 views
1 answer 212 views
...