【发布时间】:2011-10-24 20:28:13
【问题描述】:
internal static string ReadCSVFile(string filePath)
{
try
{
...
...
}
catch(FileNotFoundException ex)
{
throw ex;
}
catch(Exception ex)
{
throw ex;
}
finally
{
...
}
}
//Reading File Contents
public void ReadFile()
{
try
{
...
ReadCSVFile(filePath);
...
}
catch(FileNotFoundException ex)
{
...
}
catch(Exception ex)
{
...
}
}
在上面的代码示例中,我有两个函数ReadFile 和ReadCSVFile。
在ReadCSVFile 中,我得到了一个FileNotFoundException 类型的异常,它被catch(FileNotFoundException) 块捕获。但是当我抛出这个异常被ReadFile函数的catch(FileNotFoundException)捕获时,它会被catch(Exception)块而不是catch(FileNotFoundException)捕获。此外,在调试时,ex 的值显示为 Object Not Initialized。如何在不丢失内部异常或至少异常消息的情况下将异常从被调用函数抛出到调用函数的 catch 块?
【问题讨论】:
-
当您使用相同的异常本地对象时,您确定内部异常的类型为
FileNotFoundException? -
不是重复的,而是一个缺少信息来说明为什么它不重复的问题:)
标签: c# exception try-catch-finally