Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception not getting caught in catch block

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?

like image 837
user1501034 Avatar asked Sep 26 '12 07:09

user1501034


People also ask

Which exception Cannot be caught in catch block?

Starting with the . NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. ThreadAbortException can be caught, but will always get re-raised, so has unique behavior.

What happens if exception is not caught in try-catch?

If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

Why are exceptions not caught?

Checked exceptions must be considered at compile time, whereas unchecked exceptions are not. Exception (uppercase E) and it's subclasses are checked exceptions, which means you either have to catch any exception that can be thrown by your code, or declare the exceptions that your method could throw (if not caught).

Can exception be resolved in catch block?

The purpose of a try-catch block is to catch and handle an exception generated by working code. Some exceptions can be handled in a catch block and the problem solved without the exception being rethrown; however, more often the only thing that you can do is make sure that the appropriate exception is thrown.


3 Answers

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.

like image 164
Marc Gravell Avatar answered Oct 13 '22 00:10

Marc Gravell


Add another catch area.. You can have multiple

try
{
  // stuff
}
catch (Exception1 ex}
{
  // 1 type of exception
}
catch (Exception e)
  // catch whats left
}
like image 25
BugFinder Avatar answered Oct 13 '22 00:10

BugFinder


  • System.Exception is the mother of all exception types.So when you have it,will catch any kind of exception.
  • But when you know a specific exception is possible in your code and have a catch block with that exception type as parameter,then that block gets a higher priority over System.Exception
like image 32
Prabhu Murthy Avatar answered Oct 13 '22 01:10

Prabhu Murthy