2

在.net 中,我如何让编写子类的人意识到他们需要为基类可能抛出的特定异常类型编写代码?理想情况下,我希望强制开发人员尝试..捕获异常或在异常发生之前执行必要的验证......我是否可以使用属性来强制开发人员为异常编写代码?

例如....

我有一些调用“保存”方法的代码。在对象上调用 save 之前,我有一个提供一些验证的拦截类。如果一个类不是处于有效状态,那么我会抛出一个异常(我正在查看的不是我的代码,所以此时不​​使用异常不是一个可接受的解决方案......),我想知道的是我如何向我的代码的使用者表明他们应该检查潜在的异常并对其进行编码,或者至少执行检查以使异常不会发生......?

所以简单来说(我的代码使用了很多接口等,所以这只是一个简单的例子......)

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

因此,在此示例中,我的代码的使用者将从 SavableObject 继承如下,然后可以调用 save... 例如...

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?!
    }
}

我想知道我是否可以使用一组属性,以便我可以强制构建子类的人为异常编写代码......例如......

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() {
    }
}

任何指针都非常感谢...

编辑:澄清-我想强迫开发人员承认存在异常,这样开发人员就不能不知道异常。理想情况下,如果缺少 [ExceptionIgnored] 属性或 [ExceptionHandled](或类似)属性,编译器将停止编译......这表明已考虑到异常。我不介意忽略异常,我想做的是确保下一个开发人员知道异常的存在——如果这有意义的话。我知道在 /// 评论中我可以记录异常...

我问是因为我有几个与我们一起工作的学生不知道异常并且没有阅读所有文档,即使异常已经记录在案。我无法检查他们编写的每一行代码,所以我希望强迫他们承认异常并让编译器检查我是否考虑了异常......只要他们是,他们是否为异常编写代码是他们的选择意识到它的存在...

4

4 回答 4

4

使用 XML 文档(///方法头中的注释),特别是异常标记

该标签允许您指定可以抛出哪些异常。

让人们知道可能会发生异常就足够了。他们可以决定抓住它或让它升级。

编辑

事实上,你会喜欢有 Java 的throws关键字。不幸的是,它在 C#中不存在。

于 2012-08-25T18:26:55.860 回答
1

不应强迫开发人员在其代码中捕获和处理异常。如果他们的代码不能合理地从异常原因中恢复,则不应捕获它,并且异常应该冒泡。

于 2012-08-25T18:25:35.537 回答
1

没有办法让编译器做你要求的事情。让编译器检查对象状态正确性的最接近方法是使用代码合同http://msdn.microsoft.com/en-us/library/dd264808.aspx

于 2012-08-25T18:51:20.807 回答
1

我认为处理这种情况最简洁的方法是使用模板方法进行保存,并要求子类在抛出异常时提供错误处理程序的实现:

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();
}
于 2012-08-25T19:01:24.793 回答