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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:47:59+00:00 2026-05-27T20:47:59+00:00

I am trying to fully understand how I can simplify the following: public ActionResult

  • 0

I am trying to fully understand how I can simplify the following:

    public ActionResult Create(string ds) {
            InitializeServices(ds, "0000");
            vm.Account = new Account {
                    PartitionKey = "0000",
                    RowKey = "0000",
                    Created = DateTime.Now,
                    CreatedBy = User.Identity.Name
            };
        }
        catch (ServiceException ex) {
            ModelState.Merge(ex.Errors);
        }
        catch (Exception e) {
            Trace.Write(e);
            ModelState.AddModelError("", "Database access error: " + e.Message);
        }
        return View("CreateEdit", vm);
    }

I had a few great answers and the following was suggested:

    private void HandleException(Action action) {
        try { 
            action(); 
        } 
        catch (ServiceException ex) { 
            ModelState.Merge(ex.Errors); 
        } 
        catch (Exception e) 
        { 
            Trace.Write(e); 
            ModelState.AddModelError("", "Database access error: " + e.Message); 
        } 
    } 

    RunAndHandleExceptions(new Action(() =>                 
   {                      
        //Do some computing                 }
    )); 

This looks like a really great solution but I still don’t understand how I can pass in my
parameters into the action. What I need to do is to pass in the following:

     string ds
     System.Web.Mvc.ModelState ModelState  (passed as a reference)
  • 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-05-27T20:47:59+00:00Added an answer on May 27, 2026 at 8:47 pm

    Just

     HandleException(() => someFunction(ds, ModeState));
    

    should do it

    To get the return value, you need a Func<>, not Action<>:

    private TR HandleException<TR>(Func<TR> action)
    {
        try
        {
            return action();
        }
        catch (ServiceException ex)
        {
            ModelState.Merge(ex.Errors);
        }
        catch (Exception e)
        {
            Trace.Write(e);
            ModelState.AddModelError("", "Database access error: " + e.Message);
        }
    
        return default(TR); // null for reference types
    }
    

    You then would use it, e.g. without an existing function:

    bool result = HandleException(() =>
        {
             if (string.IsNullOrEmpty(ds))
                 return false;
    
             // do interesting stuff that throws many kinds of exceptions :)
             // Note: freely use ds and ModelState from surrounding scope, 
             // no need to 'pass them'
    
             return true;
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a basic unit of work to fully understand the principles.
I'm just trying to see if I can fully understand the concept of static
I am trying to fully understand the process pro writing code in some language
What I'm trying to do I'm trying to create a Login which is fully
I'm trying to understand why I can't specify choices to a form field's widget
I'm trying to fully understand the WebForm event model (not the page lifecycle, but
I was trying to fully understand the previous answer of the question emberjs -
I discovered to have some problem to fully understand callbacks scoping when trying to
QUESTION: What am I missing or doing wrong? I'm trying to migrate fully functional
I am trying to make my site fully w3c validator compliant. At the moment,

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.