Click here to Skip to main content
16,006,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am at 99.99% complete when scanning my filesystem but for some reason it errors at what seems like the last file in my computer. The exception is "The directory name is invalid". System.io.io.exception

[File that errors]
C:\World of Warcraft\WTF\Account\71425284#1\SavedVariables\Blizzard_CombatLog.lua.bak

[Line Of Error]
For Each file1 In .GetFiles(dir)


I have learned that it may be possible to catch the exception with
try catch ex as filenotfoundexception and have created a private sub which does not seem to catch the exception. Im puzzled as to why not

Public Sub Bypassfilenotfound()
Dim File2 As String = myScanPath.Text
Try
If My.Computer.FileSystem.FileExists(File2) = False Then
GoTo 1
End If
1: Catch ex As System.IO.FileNotFoundException
End Try
End Sub


Is my logic correct? have I missed something?
thank you in advance and sorry if this is trivial to some out there :(
Posted
Updated 24-Feb-11 13:12pm
v5

This is not wrong or right logic — this is crime.

You should not catch exceptions and block their further propagation up the stack. At the very top of the stack you should do it, however, to avoid termination. This is where you also should display the error, but maybe not to the site user; then you want to log is.

There are exclusions, but not found file is not one of them. Please see my answer on related topic (and other Answers and whole discussion) here: what are the best practices for using a try catch block[^].

—SA
 
Share this answer
 
Comments
Dale 2012 24-Feb-11 21:42pm    
Sorry for my crimes of coding but I now realize its a system.io.ioException and had nothing to do with file not found exception as i had once thought.
Sergey Alexandrovich Kryukov 24-Feb-11 22:03pm    
You're welcome!
(You crime is against you, nobody else.)
Whatever your exception is, you can always catch it as Exception type because of exception mechanism and the fact all other exceptions are derived from this type.
--SA
Dale 2012 24-Feb-11 21:43pm    
lol maybe its not my crime but i allmost bought it
Sergey Alexandrovich Kryukov 24-Feb-11 22:05pm    
If you're saying "almost" it means you did not get it and can make another crime. Hope it won't happen (I mean exception bypass.). This advice always work.
Thank you for accepting my answer.
--SA
Espen Harlinn 25-Feb-11 3:15am    
My 5 - good point - and a nice link :)
I can't remember when I last used a goto - but isn't this what you actually try to accomplish?
Public Sub Bypassfilenotfound()
  Dim File2 As String = myScanPath.Text
  Try
    If My.Computer.FileSystem.FileExists(File2) = False Then
      GoTo 1
    End If
    Catch ex As System.IO.FileNotFoundException
    End Try
1:
End Sub

Not that I agree with the coding - it's fairly "unusual" , SAKryukov has a good point :)


As I don't program in VB, but find c# to my liking - I'd probably do it like this:
public static bool MyFileExists(string filename)
{
  bool result = false;
  try
  {
    result = System.IO.File.Exist(filename);
  }
  catch(Exception exc) // a catch all - usually a bad thing, but here for simplicity
  {
    // Do something sensible here
  }
  return result;
}



you should be able to use MyFileExists as a replacement for System.IO.File.Exist. If the function has any problems checking for the existence of the file it will return false. As I mention in the comments you should do something sensible about the exceptions - like logging.

You can use log4net[^] for this purpose.

The assumption is that you want the function to return false for filenames that you will not be able to use for further processing of the file.

The method will return true for files that are found, and false if they don't, or there was any problems with the filename.

The logic should be similar for VB ...

Regards
Espen Harlinn
 
Share this answer
 
v3
Comments
Dale 2012 24-Feb-11 20:36pm    
In your code is it proper to say False = system.io.file.exist(filename)?
would that not be assuming that all files that exist are false?
I had to do a little work on getting the VB code working without any luck
is there anything else you can think of?

thank you once again! :)
Espen Harlinn 25-Feb-11 3:20am    
The method 'MyFileExists' will return true for files that are found, and false if they don't, or there was any problems with the filename
Dale 2012 24-Feb-11 21:39pm    
Thank you for your response but I have found the solution to be not catch ex as system.io.filenotfound but by simply adding a catch ex as IOException...........

Just thought id share that with other ppl that may be having the same problem because it took me many hours of fruitless searching to figure it out...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900