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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:22:33+00:00 2026-06-10T06:22:33+00:00

In .net, how do I make the guy that writes a subclass aware that

  • 0

In .net, how do I make the guy that writes a subclass aware that they need to code for a specific exception type that a base class may throw? Ideally I’m looking to force a developer to either try..catch the exception or perform necessary validation prior to the exception occuring… Can I perhaps use attributes to force a developer to code for the exception?

For example….

I have some code that calls a “Save” method. Before save is called on the object, I have an intercepting class that provides some validation. If a class is not in a valid state then I throw an exception (what I’m looking at is not my code, so not using an exception is not an acceptable solution at this point…), what I’m wondering is how I make clear to a consumer of my code that they should be checking for the potential exception and coding for it or at least performing checks so that the exception will not occur…?

So in simple terms (my code uses a lot of interfaces etc, so this is just a simple example…)

class SavableObject {
    public static void Save(){
        // validation
        ValidationClass.BeforeSave();
        // then do the save... 
        DoSave();
    }
    public static void DoSave(){
        // serialize...
    }
}

class ValidationClass {
    public static void BeforeSave(T cls){
        // perform some checks on class T
        // THROW EXCEPTION if checks fail
    }
}

So in this example a consumer of my code would inherit from SavableObject as follows and can then call save… eg…

class NewSavableThing: SavableObject
{
    public static void Save(){
        base.Save();
        // calls inherited save method which may throw an exception
        // At this point the exception may still occur, and the person writing this 
        // code may not know that the exception will occur, so the question is how do 
        // I make this clear or force the developer of this class to code for 
        // the possibility that the exception may occur?!
    }
}

I’m wondering if I could use a set of attributes such that I could force the guy building a subclass to code for the exception… eg…

class SavableObject {
    [DeveloperMustCatchException(T)] // specifies that the exception type must be caught
    public static void Save(){ ... }
}


class NewSaveableThing: SavableObject {
    [ExceptionIgnored] / [ExceptionCaught] // specifies that the developer is aware 
    // that the exception needs catching/dealing with, I am assuming that 
    // if this attribute is not provided then the compiler will catch 
    // and prevent a successful compile...
    public static void Save() {
    }
}

Any pointers much appreciated…

EDIT: to clarify – I want to force the developer to acknowledge that the exception exists, such that the developer cannot be ignorant of the exception. Ideally the compiler will stop the compilation if either a [ExceptionIgnored] attribute or an [ExceptionHandled] (or similar) attribute is missing… This would indicate that the exception has been considered. I dont mind the exception being ignored, what I’m trying to do is make sure that the next developer is aware of the existence of the exception – if that makes sense. I know that in the /// comment I can document the exception…

I ask because I have a couple of students working with us that are not aware of the exceptions and do not read all the documentation, even tho the exceptions have been documented. I cannot check every line of code they write, so I was hoping to force them to acknowledge exception and have the compiler check for me that the exception has been considered… Whether they code for the exception is their choice as long as they are aware of its existence…

  • 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-06-10T06:22:35+00:00Added an answer on June 10, 2026 at 6:22 am

    I think the cleanest way to handle this case is to use a template method to do the save and require subclasses to provide an implementation of an error handler if an exception is thrown:

    public abstract class SaveableObject
    {
        public void Save()
        {
            try
            {
                ValidationClass.BeforeSave();
                this.SaveCore();
            }
            catch(ExceptionType ex)
            {
                OnSaveException(ex);
            }
        }
    
        protected abstract void OnSaveException(ExceptionType ex);
        protected abstract void SaveCore();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a .net application where I must detect a specific object
Most of the code I've written in .NET to make REST calls have been
My situation is Using .NET 3.5 Using SDL.NET Need to make a friendly installer
I am working on an ASP.NET application that make a lot of jquery and
I know there are lots of tools on the net that can make our
i'm a .net guy doing some work/learning RoR. i have a handful of environment-specific
Hey everyone, a new guy here in C#.Net. I'm trying to make an application
http://www.mrspeaker.net/ This guy make his entire background particles but I've been roaming around in
it is a interesting question. i am using Quartz.net to make job scheduler. also
I want to make existing .NET applications (WinForms and WebForms) run on 64-bit machines,

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.