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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:07:35+00:00 2026-06-12T02:07:35+00:00

I am currently working with an ASP.NET MVC 4 project, using C#. I would

  • 0

I am currently working with an ASP.NET MVC 4 project, using C#.

I would like to create an extension method that accepts various types of methods, wraps those methods in a try/catch, and then returns the results of the method call if the call succeeds.

Here is what I have so far:

  public static object HandleServerError(this object obj)
        {
            object result = null;
            try
            {
                result = obj;
            }
            catch (Exception ex)
            {
                ErrorHandlers.ThrowServerErrorException(ex);
            }

            return result;
        } 

Here is an example of the extension method in use:

var test = webClient.DownloadString(uri).HandleServerError().ToString();

This works ok, but the webClient.DownloadString(uri) part is executed and then passed into HandleServerError as a string which is NOT what I want.

What I am wanting to do is INJECT webClient.DownloadString(uri) into the extension method so that the actual method call is wrapped into a try/catch.

Please note

I realize that there are arguments against try/catch statements, but not looking to start a debate over the subject–the core of this question is centered on the dependency injection problem. Thanks for all answers though!

  • 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-12T02:07:36+00:00Added an answer on June 12, 2026 at 2:07 am

    You need to create an extension method for a callback (a Func<T>), not an object.

    Something like this:

    static class FuncExtensions  {
        public static T HandleServerError<T>(this Func<T> func) {
            T result = default(T);
            try {
                result = func();
            }
            catch (Exception ex) {
                // Handle exception here.
            }
            return result;
        }
    }
    

    And in use it would look something like this:

    new Func<string>(() => webClient.DownloadString(uri)).HandleServerError();
    

    But this whole thing would probably be better implemented with a regular method call than extension methods.

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

Sidebar

Related Questions

Currently I'm working on Web project that is built using ASP.NET Web Forms. We
I'm currently working on an Intranet application project, using ASP.NET MVC 3. One of
I'm currently working on an ASP.NET MVC project using NHibernate and I need to
I am currently working on a ASP.NET MVC 4 Web Application project that must
I am currently working on an ASP.NET MVC project. Some developers on the team
I'm currently working on an C#/ASP.NET project that will host several differents e-commerce websites,
I am currently working on a ASP.NET MVC 3 project in which I need
I am currently working on a ASP.NET MVC 3 project in which I have
Im currently trying to refactor a project(asp.net mvc) that doesnt have any separation at
I am working on a project in ASP.NET MVC using C# 3.0. I am

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.