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中,我得到了一个FileNotFoundExceptioon类型的异常,该异常在catch(FileNotFoundException)块中被捕获。但是当我抛出这个异常被捕获到ReadFile()函数的catch(FileNotFoundException)时,它会被捕获(Exception)块而不是捕获(FileNotFoundException)。而且,在调试时,ex的值表示为Object Not Initialized。如何将被调用函数的异常抛出到调用者函数的catch块而不丢失内部异常或至少是异常消息?将被调用函数的异常抛出到调用函数的捕获块
可能重复[.NET - 引发异常的最佳实践(http://stackoverflow.com/questions/22623/net-throwing-exceptions-best-practices ) – Chris
当你使用相同的异常本地对象时,你确定内部异常的类型是'FileNotFoundException'吗? – NirMH
不是重复的,但缺乏信息的问题,以显示它为什么不重复:) – PVitt