我有尝试,渔获物和finally块的函数。如果异常被捕获,然后我捕获了异常的某些参数,如它的错误code,错误详细消息和消息,并在Excel文件打印出来。我下面张贴有关code:
I have a function with try, catch and finally block. If an exception is caught, then I catch certain parameters of that exception such as its error code, error detail message and message and print it in an excel file. Am posting relevant code below:
public void UpdateGroup(String strSiteID, String strGroup, int row)
{
try
{
Console.WriteLine("UpdateGroup");
Excel1.MWMClient.MWMServiceProxy.Group group = new Excel1.MWMClient.MWMServiceProxy.Group();
group.name = "plumber";
group.description = "he is a plumber";
Console.WriteLine(groupClient.UpdateGroup(strSiteID,group));
ExcelRecorder(0, null, null, row);
}
catch (System.ServiceModel.FaultException<DefaultFaultContract> ex)
{
ExcelRecorder(ex.Detail.ErrorCode, ex.Detail.Message, ex.Message, row);
}
finally
{
System.GC.Collect();
}
}
public void ExcelRecorder(int error, string detailmessage, string message, int row)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:/WebServiceTestCases_Output.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
if (!string.IsNullOrEmpty(message))
{
((Range)xlWorksheet.Cells[row, "M"]).Value2 = "FAIL";
((Range)xlWorksheet.Cells[row, "N"]).Value2 = error;
((Range)xlWorksheet.Cells[row, "O"]).Value2 = detailmessage;
((Range)xlWorksheet.Cells[row, "P"]).Value2 = message;
}
else
{
((Range)xlWorksheet.Cells[row, "M"]).Value2 = "PASS";
((Range)xlWorksheet.Cells[row, "N"]).Value2 = "";
((Range)xlWorksheet.Cells[row, "O"]).Value2 = "";
((Range)xlWorksheet.Cells[row, "P"]).Value2 = "";
}
xlWorkbook.Save();
xlWorkbook.Close(0,0,0);
xlApp.Quit();
}
现在的问题是,前面我有一张code像
The problem is, earlier I was having a piece of code like
catch(Exception ex)
{
ExcelRecorder(ex.Message);
}
在那个时候,所有的异常被逮住。但是,后来的要求得到了,因为我需要捕捉错误详细code和错误详细消息也发生了变化。所以,我改变了我与抓(System.ServiceModel.FaultException前)catch块,因为它让我去取那些paramaters。但现在,某些异常不被抓到在catch块。我怎样才能改变我的catch块,这样我可以捕获所有异常?
At that time, all exceptions was getting caught. But, later the requirement got changed as I needed to capture error detail code and error detail message also. So, I changed my catch block with catch (System.ServiceModel.FaultException ex) as it allowed me to fetch those paramaters. But now, certain exceptions are not getting caught in the catch block. How can i change my catch block so that I can catch all exceptions?
有两种基本方式:
1:两个抓
块(最具体的首先):
1: two catch
blocks (most specific first):
catch (System.ServiceModel.FaultException<DefaultFaultContract> ex)
{
ExcelRecorder(ex.Detail.ErrorCode, ex.Detail.Message, ex.Message, row);
}
catch (Exception ex)
{
// TODO: simpler error handler
}
2:一个抓
用试块:
catch (Exception ex)
{
var fault = ex as System.ServiceModel.FaultException<DefaultFaultContract>;
if(fault != null)
{
ExcelRecorder(fault.Detail.ErrorCode, fault.Detail.Message,
fault.Message, row);
}
// TODO: common error handling steps
}
这两者都可以正常工作。第一个可能是更清洁,但如果你想要做了很多共同的东西抓在
第二个可能有优势。
Either can work. The first is perhaps cleaner, but if you want to do a lot of common things inside the catch
the second might have advantages.