Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:41:58+00:00 2026-06-12T05:41:58+00:00

I have a function with try, catch and finally block. If an exception is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:41:59+00:00Added an answer on June 12, 2026 at 5:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
I have a code like that: User = function(){} User.a = function(){ return try;
this is so that I will have to build a delete function, I try
Today, in my C++ multi-platform code, I have a try-catch around every function. In
I have a function that takes a url to an Excel file, and then
I have a database function that retrieves data and then populates a jtable. I
I have a suspicion that I'm using the finally block incorrectly, and that I
I have a function which fetches a certain website's HTML code. Eventually that function
I would like to perform a progress indicator during request. I have try function
I try to have some inheritance with attributeBindings between two View. (function(exports) { Ember.MobileBaseView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.