locked
catch (FileNotFoundException ex) does not "catch the exception RRS feed

  • Question

  • I have implemented the following code in Visual C# 2010 express on windows 7:

    // Start code example

     if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        using (System.IO.StreamReader file2 = new System.IO.StreamReader(openFileDialog1.FileName))
                        { do a lot of stuff

                        }

                       catch (FileLoadException ex) // I have also tried (FileNotFoundException ex)
                    { // I have tried with different code in here e.g. "throw new FileLoadException();" all gives the same run time error (below)
                      
                        MessageBox.Show("The File '" + openFileDialog1.FileName + "' is locked", "File locked", MessageBoxButtons.OK, MessageBoxIcon.Error,               MessageBoxDefaultButton.Button1);
                       
                    }

    } // If

    // end code example

    The run time error I receive when I lock the file is:

    The process cannot access the file 'C:\Users\filename' because it is being used by another process.

    This is exactly the error I try to catch in order to prevent the program from crashing when a user have locked a file

    Please advice. Help will be appreciated

    Sunday, April 3, 2011 4:38 AM

Answers

  • Try this

     catch (IOException ex) // I have also tried (FileNotFoundException ex)
            { // I have tried with different code in here e.g. "throw new FileLoadException();" all gives the same run time error (below)
    
              MessageBox.Show("The File '" + openFileDialog1.FileName + "' is locked", "File locked", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
    
            }
    


    Known is a drop, Unknown is an Ocean
    • Proposed as answer by Louis.fr Monday, April 4, 2011 1:02 AM
    • Marked as answer by Aspen VJ Monday, April 11, 2011 3:23 AM
    Sunday, April 3, 2011 3:33 PM
  • Try to use a catch without specifying the exception.

    That way, it should catch every exception.

    Then, when the exception occurs, you'll be able to see in the catch block using the debugger what is the exact exception.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Aspen VJ Tuesday, April 5, 2011 3:18 AM
    • Marked as answer by Aspen VJ Monday, April 11, 2011 3:22 AM
    Sunday, April 3, 2011 8:45 AM

All replies

  • Try to use a catch without specifying the exception.

    That way, it should catch every exception.

    Then, when the exception occurs, you'll be able to see in the catch block using the debugger what is the exact exception.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Aspen VJ Tuesday, April 5, 2011 3:18 AM
    • Marked as answer by Aspen VJ Monday, April 11, 2011 3:22 AM
    Sunday, April 3, 2011 8:45 AM
  • Alternatively to what Noam B suggests, change your code to:
     
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

     

    --
    Mike
    Sunday, April 3, 2011 11:15 AM
  • Try this

     catch (IOException ex) // I have also tried (FileNotFoundException ex)
            { // I have tried with different code in here e.g. "throw new FileLoadException();" all gives the same run time error (below)
    
              MessageBox.Show("The File '" + openFileDialog1.FileName + "' is locked", "File locked", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
    
            }
    


    Known is a drop, Unknown is an Ocean
    • Proposed as answer by Louis.fr Monday, April 4, 2011 1:02 AM
    • Marked as answer by Aspen VJ Monday, April 11, 2011 3:23 AM
    Sunday, April 3, 2011 3:33 PM
  • Hi Kurt,

    The FileLoadException and FileLoadException both inherits form the IOException.

    Here is the official document about the exception:
    http://msdn.microsoft.com/en-us/library/system.io.fileloadexception.aspx
    http://msdn.microsoft.com/en-us/library/system.io.filenotfoundexception.aspx

    So you can just try the Noam's suggestion or MDM13's suggestion, both of their replies are correct. Thanks.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 5, 2011 3:22 AM
  • The "using" statement actually drills down to try/finally statement. Please try following

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        System.IO.StreamReader file2 = new System.IO.StreamReader(openFileDialog1.FileName);
                        //.... do a lot of stuff

                        }

                       catch (FileLoadException ex) // I have also tried (FileNotFoundException ex)
                    { // I have tried with different code in here e.g. "throw new FileLoadException();" all gives the same run time error (below)
                      
                        MessageBox.Show("The File '" + openFileDialog1.FileName + "' is locked", "File locked", MessageBoxButtons.OK, MessageBoxIcon.Error,               MessageBoxDefaultButton.Button1);
                       
                    }

    } // If

    // end code example

    • Proposed as answer by Vishvvas Monday, May 9, 2011 6:14 AM
    Tuesday, April 5, 2011 11:11 AM