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...