需要建议绕过未找到文件的IO异常 [英] Need Advise To Bypass File Not Found IO Exception

查看:65
本文介绍了需要建议绕过未找到文件的IO异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扫描文件系统时,我的完成率为99.99%,但由于某种原因,它似乎在计算机中的最后一个文件时出错.例外是目录名称无效". System.io.io.exception

[文件错误]
C:\魔兽世界\ WTF \帐户\ 71425284#1 \ SavedVariables \ Blizzard_CombatLog.lua.bak

[错误线]
对于.GetFiles(dir)中的每个file1


我了解到,使用
可以捕获异常 尝试将ex作为filenotfoundexception捕获,并创建了一个似乎没有捕获异常的私有子项.我不知道为什么不

公共子Bypassfilenotfound()
Dim File2 As String = myScanPath.Text
试试
如果My.Computer.FileSystem.FileExists(File2)= False,则
转到1
如果结束
1:捕获为System.IO.FileNotFoundException
结束尝试
结束子


我的逻辑正确吗?我错过了什么吗?
在此先感谢您,如果对某些人来说微不足道,请对不起:(

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 :(

推荐答案

这不是错误或正确的逻辑—这是犯罪.

您不应该捕获异常并阻止它们在堆栈中进一步传播.但是,应在堆栈的最顶部执行此操作,以免终止.您还应该在此处显示错误,但可能不会显示给网站用户;那么您想登录的是.

有排除项,但找不到文件不是其中之一.请在此处查看我对相关主题的答案(以及其他答案和整个讨论):
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


我不记得我上次使用goto的时间了-但这不是您实际上要完成的工作吗?
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


并非我同意编码-这相当不寻常",SAKryukov的观点很不错:)


由于我没有在VB中编程,而是按自己的喜好找到了C#-我可能会这样做:


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;
}




您应该可以使用MyFileExists替代System.IO.File.Exist.如果该函数在检查文件是否存在任何问题,它将返回false.正如我在评论中提到的那样,您应该对异常做一些明智的事情-例如记录日志.

您可以使用
log4net [




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


这篇关于需要建议绕过未找到文件的IO异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆