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

  • Home
  • SEARCH
  • 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 6985313
In Process

The Archive Base Latest Questions

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

In my Service classes I create an exception instance like this: protected ServiceException _ex;

  • 0

In my Service classes I create an exception instance like this:

protected ServiceException _ex;

protected void Initialize()
{
    _ex = new ServiceException();
}

Later in these classes I call this exception if something goes wrong:

    public void Delete<T, V>(T item, V repo)
        where T : Microsoft.WindowsAzure.StorageClient.TableServiceEntity
        where V : IAzureTable<T>
    {
        try
        {
            repo.Delete(item);
        }
        catch (Exception ex)
        {
            _ex.Errors.Add("", "Error when deleting " + typeof(T).Name.ToLower());
            throw _ex;
        }
    }

Outside of this in the controller I check for the exception:

    catch (Exception e) { log(e); }

Then I handle this:

    protected void log(Exception ex)
    {
        if (ex is ServiceException)
        {
            ModelState.Merge(((ServiceException)ex).Errors);  
        }
        else
        {
            Trace.Write(ex);
            ModelState.AddModelError("", "Database access error: " + ex.Message);
        }
    }

Sorry for the long example but what I would like to know is if this is a valid thing for me to be doing. In particular someone commented that I am creating a new value of _ex every time even if there’s no exception. Well my reason for doing this was that each controller has about twenty try-catch blocks and I thought it better to just create an _ex object at the start and so not have to have twenty areas where I create a new _ex if something goes wrong. I’d appreciate if someone could tell me if this all makes sense. Thanks.

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

    I would suggest collecting errors in a separate structure and then just passing it in the exception class constructor whilst throw so you do not create an instance of exception class unless really need it:

    // class constructor, List of strings or any entity like ErrorEntry
    IList<string> errors = new List<string>();
    
    // collect while execution
    errors.Add(errorText);    
    
    // raise an exception
    throw new ServiceException(this.errors);
    

    BTW, can you give some examples when you track an error but do not raising an exception?

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

Sidebar

Related Questions

I currently have service classes that look something like this public class UserService :
When I generate service classes with jax-ws it generates a static statement like this:
I have annotated my classes with @Repository, @Resource, @Component, @Service annotations but these classes
I have several service classes that have static methods and offer a service to
Can I make the spring service classes final? Is there any harm doing that?
I'm converting some services from Apache SOAP to Axis2, so the Java service classes
I have classes that are needed in both my web service and my server.
I have a property on my classes for logging service. private ILogger logger =
I have the same classes on my server and on my web service. I
I want to reload just my web layer classes without reloading my service layer

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.