2017-09-25 15 views
-4

System.Exceptionクラスですべての例外を持つことができるため、特定の例外クラスを持つ主な利点は何ですか? なぜ特定のエラー処理クラスを使用するのですか?C#例外特定のクラスの処理

+3

ちょうどインタビューから出ましたか? –

+0

答えは本当にあなたのニーズに依存しています。時には細かいことが必要な場合もあります。 –

+3

[独自のJava例外クラスを作成する必要がありますか?](https://stackoverflow.com/questions/22698584/when-should-we-create-our-own-java-exception-classes) –

答えて

1

例外ハンドラはクラス単位で動作します。あなたがやっている唯一のことは、それと再スローをログに記録されていない限り

try 
{ 
    //Do something that might raise different types of exceptions 
} 
catch(ArgumentException e1) //Catch any exception that is an ArgumentException or one its derived types 
{ 
    //Do something to handle the invalid argument 
} 
catch(NetworkException e2) //Catch any exception that is a NetworkException or one of its derived types 
{ 
    //Do something to handle the issue with the network 
} 
catch(Exception e3) 
{ 
    //Do something to log the unexpected exception 
    throw; 
} 

you should not catch the base exceptionこと:あなたが唯一の例外クラスを持っていた場合、あなたはこれを行うことができませんでした。