开发者

Should .NET optionally support "checked exceptions" in abstract methods? [closed]

开发者_如何学Python As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 12 years ago.

Should .NET provide the ability for programmers to optionally define a set of "checked exceptions" for an abstract method, or optionally in general?

I understand the reasons checked exceptions are not part of .NET and I prefer not having mandatory checked exceptions language-wide, but why not provide an optional syntax which could be useful in some situations? Take the following example, or if you just wanted to specify that some code handle certain exceptions:

public abstract class AbClass
{
    public abstract ISomeInterface AbMethod() throws AbMethodException;

    public void LargerMethod()
    {
        // some processing

        try
        {
            ISomeInterface data = AbMethod();
        }
        catch(AbMethodException ex)
        {
            // some special code
        }

        // some more processing
    }
}

Where now an implementation of AbMethod() would be required to throw AbMethodException.

public class MyAbClass : AbClass
{
    public ISomeInterface AbMethod() throws AbMethodException
    {
        // attempt to create data
        if(dataAlreadyExists)
        {
            throw new AbMethodException();
        }
        else
        {
            // create data
            ISomeInterface obj = (ISomeInterface)(new object());
            obj.WriteData();

            return obj;
        }
    }
}

Would this be a useful feature for you? Thoughts?


This might fail horribly if the correct implementation of one of your interface methods was to thow NotImplementedException. In that case, the addition of the "checked exception" does nothing but prevent someone who needs to implement the interface from being able to do so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜