コードスニペット1またはコードスニペット2はどれですか?なぜ?try catchの使用
/* Code Snippet 1
*
* Write try-catch in function definition
*/
void Main(string[] args)
{
AddMe();
}
void AddMe()
{
try
{
// Do operations...
}
catch(Exception e)
{
}
}
/* Code Snippet 2
*
* Write try-catch where we call the function.
*/
void Main(string[] args)
{
try
{
AddMe();
}
catch (Exception e)
{
}
}
void AddMe()
{
// Do operations...
}
1.ベースの 'Exception'型を決して捕まえてはいけません。 2.答えは、「AddMe」の呼び出し側がエラーについて知る必要があるかどうかによって異なります。言い換えれば、この質問に対する答えは、「あなたのエラー処理をどこでやりたいのですか?」という質問に対する答えです。 – Sven
@Sven:基本例外タイプ –
のどちらも実際には_throw_しないでください。 :) – Sven