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

The Archive Base Latest Questions

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

The .Net framework try-catch implementation only allows you to catch types which inherit off

  • 0

The .Net framework try-catch implementation only allows you to catch types which inherit off the base class “System.Exception”. Why could this not have been an interface such as “System.IException”?

Use case

We use a custom base class in each API that inherits off System.Exception. This is only thrown once an exception has been logged and therefore we can easily avoid re-logging by something like:

try
{
    // Do something.
}
catch (LoggedException)
{
    // Already logged so just rethrow.
    throw;
}
catch (Exception ex)
{
    // TODO: Log exception.
    throw new LoggedException("Failed doing something.", ex);
}

This is great until you want a custom exception that inherits off another system exception type such as System.FormatException

The only way to now handle both of these is to have two custom base types and duplicate every catch statement.

Refactoring

If the .net framework just simply looked for something such as System.IException then you could simply have a custom exception interface such as CompanyName.ILoggedException, inheriting System.IException which all your custom exception types implement. Therefore your new catch code would look something like:

try
{
    // Do something.
}
catch (ILoggedException)
{
    // Already logged so just rethrow.
    throw;
}
catch (IException ex)
{
    // TODO: Log exception.
    throw new CustomException("Failed doing something.", ex);
}

Is there a practical reason the framework is implemented like this? Or would this be something to request in a future version of the .Net framework?

  • 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-27T05:13:26+00:00Added an answer on May 27, 2026 at 5:13 am

    One possibly neater workaround that hasn’t been mentioned yet is to use extension methods. By harnessing the Exception.Data field, you could neatly discover from a single catch block whether the current exception has been logged yet, and take action as required. This would allow you to construct numerous different company specific exceptions (which are implicitly already logged).

    Extension methods required:

    private const string ExceptionLoggedKey = "IsExceptionLogged";
    
    public static bool IsLogged(this Exception exception)
    {
        if (exception.Data.Contains(ExceptionLoggedKey))
        {
            return (bool)exception.Data[ExceptionLoggedKey];
        }
        return false;
    }
    
    public static void SetLogged(this Exception exception)
    {
        exception.Data.Add(ExceptionLoggedKey, true);
    }
    

    Company exceptions take the following format, setting the IsLogged flag in the constructor:

    public class CompanysLoggedException : InvalidOperationException  //Could be any Exception
    {
        public CompanysLoggedException()
        {
            this.SetLogged();
        }
    }
    

    try/catch usage:

    try
    {
        throw new ArgumentException("There aren't any arguments.");
    }
    catch (Exception ex)
    {
        if (ex.IsLogged())
            //Nothing additional to do - simply throw the exception
            throw;
        else
            //TODO Log the exception
            throw new CompanysLoggedException();
    }
    

    I’d agree that this definitely isn’t as neat as the ability to match an exception based on an implemented interface, but I think the pattern is quite concise and readable. Having to remember to add the call to SetLogged() to each new company exception defined is a bit of a flaw though.

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

Sidebar

Related Questions

.Net framework contains a great class named Convert that allows conversion between simple types,
I have an Asp.NET application (VS2008, Framework 2.0). When I try to set a
.NET framework 3.5 introduces Action and Func<> predefined delegate types that are very handy.
The .net framework provides in the Math class a method for powering double. But
The .net framework 4 is apparently going to include a BigInteger class. However, I
I sometimes see methods in the .net framework prefixed with Try e.g. int.TryParse(..). I
I am writing an ASP.NET website, which is a new framework for me. I
As we all know, .NET Framework provides a little bit specific implementation of ThreadAbortException
I am using ASP.net's AJAX framework and I am trying to do some exception
I'm trying to catch this exception: System.ServiceModel.EndpointNotFoundException But visual studio isn't finding it. There's

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.