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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:49:29+00:00 2026-05-30T15:49:29+00:00

This is my first application that will deal with exceptions properly. It’s a WCF

  • 0

This is my first application that will deal with exceptions properly. It’s a WCF service. All the other before were simple apps just for myself. I have very small knowledge about exception handling in C#.

I have a code like this:

MembershipUser memUser = Membership.GetUser(username);

DatabaseDataContext context = new DatabaseDataContext();
UserMembership user = UserMembership.getUserMembership(memUser);
ItemsForUser itemUser = Helper.createItemForUser(ref item, memUser);
Helper.setItemCreationInfo(ref item, user);
context.Items.InsertOnSubmit(item);
context.SubmitChanges();

In this code a few exceptions can happen. Like NullReferenceException for example. How do I know which object caused the exception so I can know what to do in the catch and what to return to the client?

  • 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-30T15:49:31+00:00Added an answer on May 30, 2026 at 3:49 pm

    In general, you should not catch any exceptions.

    I know this sounds strange, but the fact is that you should only catch exceptions you can actually do something about, and you usually can’t do anything about exceptions.

    The “exceptions” to this rule have to do with what it means to “handle” an exception. In some applications, “handling” the exception means to log it. In others (ASP.NET, for instance), it’s better to not handle the exception, because the framework (ASP.NET Health Monitoring in this case) will log it for you.

    In event-driven code, like Windows Forms, I find it’s necessary to catch exceptions within event handlers. At least in earlier versions of .NET, allowing the exception to propagate outside of, for instance, a button click event produced unpleasant results. I typically catch the exception and display it in a dialog box.

    In a multi-tier application, one might catch exceptions on tier boundaries, then rethrow a new, tier-specific exception:

    try
    {
       // ...
    }
    catch (Exception ex)
    {
        throw new TierException("Some message", ex);
    }
    

    Another use case is to catch the exception, then throw a new exception with more information in it:

    public int GetValueFromConfigurationFile(...)
    {
        const string configFileName = "...";
        try
        {
            // ...
        }
        catch (FileNotFoundException fEx)
        {
            throw new InvalidOperationException(
                String.Format("Can't find configuration file {0}", configFileName), 
                fEx);
        }
    }
    

    In this case, you are catching a specific exception (FileNotFoundException), and providing your caller information they could not otherwise know: that the file not found was a configuration file.

    The general message is:
    – Only catch exceptions you know how to handle
    – Catch the most specific exception possible
    – Always include the inner exception when creating a new exception, to preserve the chain of exceptions
    – To rethrow the current exception, use throw;, not throw ex;

    There are a few more, and I’ll try to find you the reference from the Microsoft Framework Design Guidelines.

    P.S. If someone can find the question this is a duplicate of, feel free to close as a duplicate. I don’t mind losing any rep from this. I just couldn’t find the duplicate.


    I should have just posted the link to the “exception-handling” tag wiki, where it says:

    However, for exception handling in the context of .NET programs, see
    “Design Guidelines for
    Exceptions
    “.

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

Sidebar

Related Questions

I am writing an API for my ASP.NET application that other developers will use.
I am using Flex Builder 3 to develop my first application that will communicate
This is my first major application using multiple classes. It is written in vb
I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
I'm writing an application using DDD techniques. This is my first attempt at a
I have an application. First I display a splash screen, a form, and this
First of all I'd welcome edits to the title of this question, I couldn't
I am a new developer and this is my first time to deal with
I have realized that my first question won't be answerable, since php can't deal
I'm writing an application that displays posts, much like a simple forum system. Posts

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.