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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:13:11+00:00 2026-05-27T18:13:11+00:00

I currently handle my exceptions like this: try { } catch (ServiceException ex) {

  • 0

I currently handle my exceptions like this:

try {

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

This works but it’s the same code I repeat many times. What I am looking for is some suggestion on how I could move this into an external function. I don’t necessarily need to move the try block there but at least the other code.

Maybe a function that was passed the Exception and the ModelState (as a reference). Can anyone suggest a clean way that I could code up this function. I’m asking here because almost always someone seems to come up with a solution that I could never have thought of. Thanks Samantha.

  • 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-27T18:13:12+00:00Added an answer on May 27, 2026 at 6:13 pm

    You could make a method that takes in an Action, and invokes it in a try/catch block:

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

    And call it like this:

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

    EDIT: with a parameter (example, can run in a console program):

    private static void ParameterizedTask()
        {
            Task.Factory.StartNew(new Action<object>((y) =>
            {
                Console.WriteLine(y);
            }), 5);
            Thread.Sleep(1500);
        }
    //OUTPUT: 5
    

    For more info you can take a look at this thread.

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

Sidebar

Related Questions

currently i am relying on a proxy script to handle this problem of Single
The code currently does this and the fgetpos does handle files larger than 4GB
I would like to get a second opinion on how to handle Exceptions within
Hi All, I am currently binding my data in a datagrid like this public
What is a decent way to handle PDO error when using try catch block?
How can I correctly handle exceptions thrown from controllers in ASP.NET MVC? The HandleError
We're struggling with a policy to correctly handle exceptions in our application. Here's our
First let me explain how I currently handle validation, say, for an IPv4 address:
I'm currently writing a class to handle all database-activity in my application, and so
I'm currently trying to implement a class to handle secure communications between instances of

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.