LoginSignup
20
13

More than 5 years have passed since last update.

C#のthrow eは使うな!

Posted at

はじめに

C#では、throw eで、例外を再スローすると、スタックトレースが上書きされるので、throw eは使わないほうが良いです。

サンプルで説明

using System;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Method2();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private static void Method2()
        {
            try
            {
                Method1();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private static void Method1()
        {
            throw new Exception("エラー発生");
        }
    }
}

上記を実行すると、コンソールには以下のように出力されます。

System.Exception: エラー発生
   at csharp.Program.Method2() in D:\dev\qiita\csharp\Program.cs:line 27
   at csharp.Program.Main(String[] args) in D:\dev\qiita\csharp\Program.cs:line 11

発生元(スタックトレースの一番上)が、Method2になっていますね。
実際には、Method1で発生しているのですが、throw eのせいで、スタックトレースが上書きされています。

C#で、スタックトレースを上書きされないように再スローするには、以下のようにMethod2を変更すればOKです。

        private static void Method2()
        {
            try
            {
                Method1();
            }
            catch
            {
                throw;
            }
        }

このように、throwだけであれば、以下のようにスタックトレースが上書きされなくなります。

System.Exception: エラー発生
   at csharp.Program.Method1() in D:\dev\qiita\csharp\Program.cs:line 33
   at csharp.Program.Method2() in D:\dev\qiita\csharp\Program.cs:line 27
   at csharp.Program.Main(String[] args) in D:\dev\qiita\csharp\Program.cs:line 11

おわりに

私が今まで経験したプロジェクトにおいて、スタックトレースの上書きが必要であったことは、一度もありません。
JavaやJavaScriptでは、同様の記述をしてもスタックトレースは上書きされないので、C#でなぜこのような仕様になっているのか、調査をしても私には分かりませんでした。

もし、C#がthrow eでスタックトレースを上書きする理由をご存知の方がいらっしゃいましたら、ぜひ、コメントで教えてください!

20
13
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
13