New*_*der 2 c# oop design-principles solid-principles
它说Derived类不应抛出Base类不知道的任何异常,我试图找到它的工作原理,在基类中我抛出System.Exception并在Derived中抛出ArgNullException().有人可以解释这是好的
class b
{
virtual public void foo()
{
try
{
if (true)
throw new System.Exception();
}
catch (Exception ex)
{
Console.WriteLine("in base");
}
}
}
class a : b
{
override public void foo()
{
try
{
if (true)
throw new ArgumentNullException();
}
catch (Exception ex)
{
Console.WriteLine("in dervied");
}
}
}
Run Code Online (Sandbox Code Playgroud)
class MyClass
{
public virtual void Foo()
{
if (true)
throw new System.Exception();
}
}
}
class MyDerivedClass : MyClass
{
public override void Foo()
{
if (true)
throw new ArgumentNullException();
}
}
}
public class Program
{
public static void Main()
{
try
{
// a factory creating the correct
// MyClass derived instance
var myClass = someFactory.Create();
myClass.Foo();
}
catch (Exception)
{
// will work.
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以在基类中抛出最少的特定异常(为什么这很糟糕是另一个讨论).就像这样,任何使用子类的人都能够捕获它,无论你抛出多么具体的异常.
让我们说它反过来了.基类抛出ArgumentNullException和子类Exception.现在,任何只知道基类的人都只会有捕获块,ArgumentNullException因为这是他们所期望的.因此,当子类抛出时,它们的应用程序将失败Exception.
class MyClass
{
public virtual void Foo()
{
if (true)
throw new ArgumentNullException();
}
}
}
class MyDerivedClass : MyClass
{
public override void Foo()
{
if (true)
throw new Exception();
}
}
}
public class Program
{
public static void Main()
{
try
{
// a factory creating the correct
// MyClass derived instance
var myClass = someFactory.Create();
myClass.Foo();
}
catch (ArgumentNullException)
{
// won't work since the subclass
// violates LSP
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
836 次 |
| 最近记录: |