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 8331621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:28:07+00:00 2026-06-09T02:28:07+00:00

I have a particular situation where I need to trap exceptions and return an

  • 0

I have a particular situation where I need to trap exceptions and return an object to the client in place of the exception. I cannot put the exception handling logic at a higher level i.e. wrap Foo within a try clause.

It’s best to demonstrate with some sample code. The exception handling logic is clouding the intention of the method and if I have, many methods of similar intent, in the Foo class, I find myself repeating most of the catch logic.

What would be the best technique to wrap the common exception functionality in the code below?

public class Foo
{
     public Bar SomeMethodThatCanThrowExcepetion()
     {
          try
          {
              return new Bar().Execute();
          }
          catch(BazException ex)
          {
              WriteLogMessage(ex, Bar.ErrorCode);
              return new Bar() { ErrorMessage = ex.Message, ErrorCode = Bar.ErrorCode;}                  
          }
     }

     public Baz SomeMethodThatCanThrowExcepetion(SomeObject stuff)
     {
          try
          {
              return new Baz(stuff).Execute();
          }
          catch(BazException ex)
          {
              WriteLogMessage(ex, Baz.ErrorCode);
              return new Baz() { ErrorMessage = ex.Message, ErrorCode = Baz.ErrorCode;}                  
          }
     }
 } 
  • 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-09T02:28:09+00:00Added an answer on June 9, 2026 at 2:28 am

    Updated per Lee’s comment


    One possibility is to use a generic helper method. Something like this:

    T TryExecute<T>(Func<T> action, int ErrorCode)
    {
        try
        {
            return action();
        }
        catch (Exception ex)
        {
            result = Activator.CreateInstance<T>();
            typeof(T).GetProperty("ErrorMessage").SetValue(result, ex.Message, null);
            typeof(T).GetProperty("ErrorCode").SetValue(result, ErrorCode, null);
            return result;
        }
        return result;
    }
    

    If you can modify Bar and Baz, then you could improve this by placing a requirement on T:

    public interface IError
    {
        public string ErrorMessage { get; set; }
        public int ErrorCode { get; set; }
    }
    
    T TryExecute<T>(Func<T> action, int ErrorCode) where T : IError
    {
        try
        {
            return action();
        }
        catch (Exception ex)
        {
            result = Activator.CreateInstance<T>();
            result.ErrorMessage = ex.Message;
            result.ErrorCode = ErrorCode;
            return result;
        }
    }
    

    Then you’d use:

    return TryExecute<Bar>(new Bar().Execute, Bar.ErrorCode);
    

    And:

    return TryExecute<Baz>(new Baz(stuff).Execute, Baz.ErrorCode);
    

    That may or may not be an over-abstraction for your particular design; the devil is in the details.

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

Sidebar

Related Questions

In a particular situation I need to have variable (character array or std:string) where
I have a particular situation: a service started by a broadcast receiver starts an
In my particular situation, I have a couple of solutions to my problem. I
I have a situation where particular elements (xs:simpleType, xs:complexType) are placed in the output
Here's the situation: On a particular branch, some changes have been merged in from
Here's my particular situation... I have a decent amount of experience with webforms, and
In my code, I have a situation where I need to copy data from
I have an error/exception that is thrown by a particular method. Any time this
If I have a business object with 50 fields, and I need to populate
I'm in need to lookup the key from a particular element. It doesn't have

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.