Should .NET optionally support "checked exceptions" in abstract methods? [closed]
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.
精彩评论