C# Exception Handling Practices に関する他のいくつかの質問を読みましたが、探しているものを尋ねるものはないようです。
特定のクラスまたはクラスのセットに対して独自のカスタム例外を実装する場合。これらのクラスに関連するすべてのエラーは、内部例外を使用して例外にカプセル化する必要がありますか?
ソースから例外をすぐに認識できるように、すべての例外をキャッチする方がよいと考えていました。私はまだ元の例外を内部例外として渡しています。一方、例外を再スローするのは冗長だと思っていました。
例外:
class FooException : Exception
{
//...
}
オプション 1: Foo はすべての例外をカプセル化します:
class Foo
{
DoSomething(int param)
{
try
{
if (/*Something Bad*/)
{
//violates business logic etc...
throw new FooException("Reason...");
}
//...
//something that might throw an exception
}
catch (FooException ex)
{
throw;
}
catch (Exception ex)
{
throw new FooException("Inner Exception", ex);
}
}
}
オプション 2: Foo は特定の FooExceptions をスローしますが、他の例外を通過させます。
class Foo
{
DoSomething(int param)
{
if (/*Something Bad*/)
{
//violates business logic etc...
throw new FooException("Reason...");
}
//...
//something that might throw an exception and not caught
}
}