gpt4 book ai didi

c# - 获取 InnerException 导致错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:01 26 4
gpt4 key购买 nike

在我多年的编程中,我从未见过这种情况发生,不得不问问是否有其他人知道这是为什么......

            Excel.Worksheet worksheet;
Excel.Sheets sheets;
Excel.Workbook theWorkbook;
Excel.Application ExcelObj = null;

try
{
ExcelObj = new Excel.Application();
worksheet = new Excel.Worksheet();
string sheetName;

theWorkbook = ExcelObj.Workbooks.Open(txtImportFilePath.Text, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, true, 0, true, 1, 0);
sheets = theWorkbook.Worksheets;


}
catch (Exception ex)
{
MessageBox.Show("An error occured: " + ex.InnerException.ToString);
}
finally
{
worksheet = null;
sheets = null;
theWorkbook = null;
ExcelObj.Quit();
ExcelObj = null;
}

catch block 本身导致程序因错误而崩溃。实际错误发生在行上: theWorkbook = ExcelObj.Workbooks.Open(.....它试图打开的文件已损坏或扩展名不匹配错误。

但是当消息框试图显示时,有一个对象引用未设置为实例错误。

这不是因为消息框。这是因为 ex.InnerException。我尝试做一些不同的事情,它在 ex.InnerException 行抛出了错误:

Catch(Exception ex)
{
string err;
err = ex.InnerException; //Object reference not set to an instance.....
}

我从未见过这种情况。有什么线索或建议吗?

谢谢!!!

最佳答案

问题是您收到的 Exception 没有内部异常。

你应该检查一下:

catch (Exception ex)
{
if (ex.InnerException != null)
MessageBox.Show("An error occured: " + ex.InnerException.ToString());
else
MessageBox.Show("An error occured: " + ex.ToString());
}

关于c# - 获取 InnerException 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com