Reputation: 363
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?
Upvotes: 6
Views: 2331
Reputation: 9261
System.Exception
is the mother of all exception types.So when you
have it,will catch any kind of exception.System.Exception
Upvotes: 2
Reputation: 397
Have as much catch blocks, for each of the expected exceptions. Dont forget to catch the most specific ones on top. Finally catch the Exception
class to catch any of the remaining exceptions.
If you catch the Exception
on top, for any exception, this block will fire and all other blocks will be unreachable.
try
{
// your code here
}
catch (FirstSpecificException ex)
{
}
catch (SecondSpecificException ex)
{
}
catch (NthSpecificExceptoin ex)
{
}
catch (Exception ex)
{
// in case you might have missed anything specifc.
}
Upvotes: 1
Reputation: 38200
So from what you mentioned it seems like you have
try{}
catch(FaultException ex){ExcelRecorder(ex.Message,[other params]);}
Now you can have one more catch block for all other exceptions like
catch(Exception all){// you may log}
so when a different exception arises it would not be handled by the FaultException
catch but instead move into the generic exception block and you can choose to process it as you need
Upvotes: 1
Reputation: 1062502
There are basically two ways:
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: one catch
block with a 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
}
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.
Upvotes: 9
Reputation: 14581
You can do one of the following:
catch
block for each exception you are interested incatch Exception ex
to catch all and pick only those you are interested inIn general, you either catch all exceptions (option 2), or only those that you really know how to handle (option 1)
Upvotes: 1
Reputation: 17858
Add another catch area.. You can have multiple
try
{
// stuff
}
catch (Exception1 ex}
{
// 1 type of exception
}
catch (Exception e)
// catch whats left
}
Upvotes: 3