Exception not caught in catch catch

I have a function with try, catch and finally block. If an exception is caught, I will catch certain parameters of this exception, such as its error code, error message and message, and print it in the excel file. Send the appropriate 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 that I used to have a piece of code like

 catch(Exception ex) { ExcelRecorder(ex.Message); } 

At that time, all exceptions were caught. But, later the requirement was changed, because I also had to write down the error code and a detailed error message. So, I changed my catch block to catch (System.ServiceModel.FaultException ex), as it allowed me to get these parameters. But now, some exceptions do not fall into the catch block. How can I change my catch block so that I can catch all exceptions?

+6
c # exception try-catch winforms
source share
6 answers

There are two ways:

1: two catch blocks (the 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: one catch block with test:

 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 } 

You can work. The former may be cleaner, but if you want to do a lot of common things inside catch , the latter may have advantages.

+9
source share

Add another capture area. You may have several

 try { // stuff } catch (Exception1 ex} { // 1 type of exception } catch (Exception e) // catch whats left } 
+3
source share
  • System.Exception is the mother of all types of exceptions. So when you have this, catch any exception.
  • But when you know what specific exception is possible in your code and have a catch block with this type of exception as a parameter, then this block gets a higher priority over System.Exception
+2
source share

You can do one of the following:

  • Provide a separate catch for each exception you are interested in.
  • use catch Exception ex to catch everything and select only those that interest you.
  • catch the base exception class of the exception family that interests you if there is such a base class (but usually it isn’t)

In general, you will either catch all exceptions (option 2), or only those that you really know how to handle (option 1)

+1
source share

So, from what you mentioned, it looks like you have

 try{} catch(FaultException ex){ExcelRecorder(ex.Message,[other params]);} 

You can now have another catch block for all other exceptions like

 catch(Exception all){// you may log} 

therefore, when another exception occurs, it will not be handled by the catch of the FaultException , but instead will go into the general exception block, and you can handle it as you need

+1
source share

Have as many catch blocks for each of the expected exceptions. Remember to catch the most specific at the top. Finally, catch the Exception class to catch any of the remaining exceptions.

If you catch an Exception from above, for any exception this block will be launched, and all other blocks will be inaccessible.

 try { // your code here } catch (FirstSpecificException ex) { } catch (SecondSpecificException ex) { } catch (NthSpecificExceptoin ex) { } catch (Exception ex) { // in case you might have missed anything specifc. } 
+1
source share

All Articles