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(); } The problem is, earlier I was having a piece of code like
catch(Exception ex) { ExcelRecorder(ex.Message); } 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?