How to Do File Exception Handaling in C#

Questions by Krishna125

Showing Answers 1 - 6 of 6 Answers

Ranjit

  • Aug 28th, 2008
 

Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution

ronivars

  • May 13th, 2010
 

 

        private static void OpenFile()
        {
            FileStream fs = null;
            try
            {
                fs = File.OpenRead(@"c:some file.log");             
            }
            catch (FileNotFoundException fnfe)
            {
                Debug.WriteLine(fnfe.Message);
            }
            catch (FieldAccessException fe)
            {
                Debug.WriteLine(fe.Message);
            }
            catch (FileLoadException fle)
            {
                Debug.WriteLine(fle.Message);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions