-2

あなたはどのソリューションを好みますか?私は解決策2を好みますが、より良い方法が存在するかどうか疑問に思っています

解決策 1 - ネストされた try/catch

try
{
   //some code that could throws Exception1
   try
   {
        //some code that could throws Exception2
        return success;
   }      
   catch (Exception2 e)
   {
      return failure;
   }
}
catch (Exception1 e)
{
  return failure;
}    

解決策 2 - 順次試行/キャッチ

try
{
   //some code that could throws Exception1 
}
catch (Exception1 e)
{
  return failure;
}

try
{
    //some code that could throws Exception2
}      
catch (Exception2 e)
{
  return failure;
}

return success;
4

2 に答える 2

1

-4! 特に始めたばかりの人にとっては、それは合理的な質問だと思います。どうしてそんなに悪い質問なの?

個人的には、やむを得ない理由がない限り、以下のコードを使用します。

try
{
   //some code that could throws Exception1 
   //some code that could throws Exception2
   //some code that could throws Exception3
}
catch (Exception e)
{
  return failure;
}
于 2013-03-09T11:49:45.570 に答える
0

あなたが試すことができます

try
{
   //some code that could throws Exception1 
   //some code that could throws Exception2
}
catch (Exception1 e)
{
   return failure;
}
catch (Exception2 e)
{
   return failure;
}

return success;
于 2013-03-09T11:49:13.120 に答える