简体   繁体   中英

GetFileInfo resulting in First-chance exception

I have a try/catch block where I use a GetFileInfo to get the size of a specific file. I'm getting an exception shown in the output window of debugger (although the program still continues its run without reacting to the exception in any way).

The error message I get:

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll

My question here is twofold:

  1. Why does the error occur / what can I do to avoid it?
  2. Why does the exception show up in the output despite try/catch? Is this because an external dll throws it?

Code:

try
{
    FileInfo fileInfo = new FileInfo(filePath);
    return fileInfo.Length;
}
catch
{
}
return 0;

The above code is in a method that is called and the filePath is c:\\test\\test.txt and the file exists at that location.

  1. It smells a lot like you forgot to escape the \\ s in your path ;-)
  2. As it is always interesting to see exceptions happening in your program, this is indeed the perfectly regular behaviour of the debuggers output window.

在VS中,可以选择“ 调试” ->“ 异常”,并在引发特定类型的异常时关闭自动中断。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM