我已经创建了一个代码。理想的代码是: -使用excel文件进行异常处理
- 它执行函数GetTaskStatus()。我的代码中还有其他几个类似的功能。
- 如果任何函数发生异常,它将控件移动到ExcelRecorder()函数
- 如果在任何函数中都有这样的异常,我必须在Excel表格中写入FAIL,在给定行的J单元格以及同一行的K单元格中的确切异常错误(例如,,NullReferenceException被发现为)。总之,J细胞是RESULT,K细胞是REMARKS。我的Excel表中有几行。
- 如果发生任何功能也不例外,我只需要编写PASS在给定行的歼细胞
- 我的代码是能够做到这一点。但是,但是,有一个问题。无论是否发生异常,我的代码都为我的Excel中的所有行输入FAIL和REMARKS(虽然某些功能不会导致任何异常)
我已发布我的代码的主要代码片段。任何人都可以让我知道我要去哪里吗?如果函数没有发生异常,我需要输入PASS。
CODE低于
public void GetTaskStatus()
{
try
{
Console.WriteLine("Invoking GetTaskStatus method");
Console.WriteLine("------------------****-----------------");
m_taskStatus = taskClient.GetTaskStatus(m_taskID);
Console.WriteLine("Task status : " + m_taskStatus.taskStatus.ToString());
Console.WriteLine("-----------------------------------");
}
catch (Exception ex)
{
MessageBox.Show("An exception has occured. Please check the Excel sheet for more info", "Exception Caught" + ex);
ExcelRecorder(true, ex.Message);
}
finally
{
GC.Collect();
}
}
public void ExcelRecorder(bool isExceptionalData, string message)
{
MessageBox.Show("ExcelRecorder method reached when exception occurs");
//Code for recording into excel should be written here
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/dsds.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
int numSheets = xlWorkbook.Sheets.Count;
for (int row = 2; row <= rowCount; row++)
{
if (isExceptionalData)
{
((Range)xlWorksheet.Cells[row, "J"]).Value2 = "FAIL";
((Range)xlWorksheet.Cells[row, "K"]).Value2 = message;
}
else
{
((Range)xlWorksheet.Cells[row, "J"]).Value2 = "PASS";
}
}
xlWorkbook.Save();
xlWorkbook.Close(0,0,0);
xlApp.Quit();
}
使用调试器来查看为什么捕获到异常。 Ctrl-Alt-E弹出“例外”对话框。选中所有框以“抛出中断”。 – sehe