LoginSignup
5
5

More than 5 years have passed since last update.

【C#】例外チェインでわかるExceptionクラスの要素(スタックトレースとか)

Last updated at Posted at 2018-01-06

はじめに

C#のExceptionクラスの各プロパティとメソッドの内容をExceptionを順にチェインで結んだプログラムを実行し解読します。

具体的にはC#例外クラス(Exception)の以下のプロパティ、メソッドの中身をコードを通して探ります。

  • GetType() メソッド
  • Source プロパティ
  • StackTrace プロパティ
  • GetBaseException() メソッド
  • ToString() メソッド

実装

以下に3つ例外がチェインとして繋がるようにエラーハンドリングし、例外クラスの要素を出力するコードを示します。

exception_sample.cs
using System;

namespace NDP_UE_CS
{
    // 2つの派生Exceptionをネストされた例外の
    // 動きをみるためのデモ用に定義する
    class SecondLevelException : Exception
    {
        public SecondLevelException( string message, Exception inner )
            : base( message, inner )
        { }
    }
    class ThirdLevelException : Exception
    {
        // 前Exceptionを受け取らないコンストラクタ
        public ThirdLevelException( string message )
            : base( message )
        { }

        public ThirdLevelException( string message, Exception inner )
            : base( message, inner )
        { }
    }

    class NestedExceptions
    {
        public static void Main()
        {
            Console.WriteLine(
                "\nこのプログラムは0ディビジョンを行います。このとき" +
                "異なった派生Exceptionを用いて\n" +
                "それぞれ例外を2回以上発生させます。\n");

            try
            {
                // この`Rethrow`関数は0ディビジョンを行う
                // 別関数を呼び出します。
                Rethrow( );
            }
            catch( Exception ex )
            {
                Exception current;

                Console.WriteLine(
                    "ネストされた例外をInnerExceptionプロパティを" +
                    "用いて紐解きます。\n" );

                // このコードではネストされた例外をInnerExceptionプロパティを
                // 用いて紐解きます
                current = ex;
                while( current != null )
                {
                    Console.WriteLine(string.Format("--- {0} の処理を開始 ---", current.GetType().ToString()));
                    Console.WriteLine( );

                    Console.WriteLine("Exception.Source プロパティを出力します。");
                    Console.WriteLine( current.Source.ToString( ) );
                    Console.WriteLine( );

                    Console.WriteLine("Exception.StackTrace プロパティを出力します。");
                    Console.WriteLine( current.StackTrace.ToString( ) );
                    Console.WriteLine( );

                    Console.WriteLine("Exception.GetBaseException() メソッドを出力します。");
                    Console.WriteLine( current.GetBaseException().ToString( ) );
                    Console.WriteLine( );

                    Console.WriteLine("Exception.ToString() メソッドを出力します。");
                    Console.WriteLine( current.ToString( ) );
                    Console.WriteLine( );

                    Console.WriteLine(string.Format("--- {0} の処理を終了 ---", current.GetType().ToString()));
                    Console.WriteLine( );

                    current = current.InnerException;
                }
            }
        }

        // `Rethrow`関数は`DivideBy0( )`関数からの例外をキャッチし
        // 別の例外を生成します。
        static void Rethrow()
        {
            try
            {
                DivideBy0( );
            }
            catch( Exception ex )
            {
                /*
                throw new ThirdLevelException(
                    "2番目の例外をキャッチせずに" +
                    "3番目の例外を投げます。");
                */
                throw new ThirdLevelException(
                    "2番目の例外をキャッチし" +
                    "3番目の例外を投げます。", ex);
            }
        }

        // `DivideBy0`関数は0ディビジョンを実行し
        // 2番目の例外を生成します。
        static void DivideBy0( )
        {
            try
            {
                int  zero = 0;
                int  ecks = 1 / zero;
            }
            catch( Exception ex )
            {
                throw new SecondLevelException(
                    "0ディビジョン例外をキャッチし" +
                    "2番目の例外を投げます。", ex );
            }
        }
    }
}

出力


このプログラムは0ディビジョンを行います。このとき異なった派生Exceptionを用いて
それぞれ例外を2回以上発生させます。

ネストされた例外をInnerExceptionプロパティを用いて紐解きます。

--- NDP_UE_CS.ThirdLevelException の処理を開始 ---

Exception.Source プロパティを出力します。
exception_sample

Exception.StackTrace プロパティを出力します。
  at NDP_UE_CS.NestedExceptions.Rethrow () [0x00016] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
  at NDP_UE_CS.NestedExceptions.Main () [0x0000a] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

Exception.GetBaseException() メソッドを出力します。
System.DivideByZeroException: Attempted to divide by zero.
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

Exception.ToString() メソッドを出力します。
NDP_UE_CS.ThirdLevelException: 2番目の例外をキャッチし3番目の例外を投げます。 ---> NDP_UE_CS.SecondLevelException: 0ディビジョン例外をキャッチし2番目の例外を投げます。 ---> System.DivideByZeroException: Attempted to divide by zero.
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
   --- End of inner exception stack trace ---
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00017] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
  at NDP_UE_CS.NestedExceptions.Rethrow () [0x00000] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
   --- End of inner exception stack trace ---
  at NDP_UE_CS.NestedExceptions.Rethrow () [0x00016] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
  at NDP_UE_CS.NestedExceptions.Main () [0x0000a] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

--- NDP_UE_CS.ThirdLevelException の処理を終了 ---

--- NDP_UE_CS.SecondLevelException の処理を開始 ---

Exception.Source プロパティを出力します。
exception_sample

Exception.StackTrace プロパティを出力します。
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00017] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
  at NDP_UE_CS.NestedExceptions.Rethrow () [0x00000] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

Exception.GetBaseException() メソッドを出力します。
System.DivideByZeroException: Attempted to divide by zero.
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

Exception.ToString() メソッドを出力します。
NDP_UE_CS.SecondLevelException: 0ディビジョン例外をキャッチし2番目の例外を投げます。 ---> System.DivideByZeroException: Attempted to divide by zero.
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
   --- End of inner exception stack trace ---
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00017] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 
  at NDP_UE_CS.NestedExceptions.Rethrow () [0x00000] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

--- NDP_UE_CS.SecondLevelException の処理を終了 ---

--- System.DivideByZeroException の処理を開始 ---

Exception.Source プロパティを出力します。
exception_sample

Exception.StackTrace プロパティを出力します。
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

Exception.GetBaseException() メソッドを出力します。
System.DivideByZeroException: Attempted to divide by zero.
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

Exception.ToString() メソッドを出力します。
System.DivideByZeroException: Attempted to divide by zero.
  at NDP_UE_CS.NestedExceptions.DivideBy0 () [0x00002] in <bb901cae5fe14264ad212a7e9f2c60f9>:0 

--- System.DivideByZeroException の処理を終了 ---


解説

上記コードには以下3つの例外が存在

  1. ThirdLevelException (ユーザー定義)
  2. SecondLevelException (ユーザー定義)
  3. DivideByZeroException (標準例外)

ThirdLevelException(3番目の例外)が持つinnerExceptionがSecondLevelException(2番目の例外)であり、
SecondLevelExceptionが持つinnerExceptionがDivideByZeroException例外
という関係です。

GetType()メソッド

3つの各例外のクラス名
NDP_UE_CS.ThirdLevelException, NDP_UE_CS.SecondLevelException, System.DivideByZeroException

Sourceプロパティ

3つの各例外共通でファイル名
exception_sample

StackTraceプロパティ

3つの各例外ごとに例外呼び出し元の関数からキャッチされるまでを持つ

GetBaseException()メソッド

3つの各例外共通で一番最下System.DivideByZeroException例外

ToString()メソッド

自身と自身の持つInnerException例外のスタックトレースを再帰的に出力する。

まとめ

  • 基本的に次に新たな例外を投げるときはキャッチしている例外をコンストラクタに渡すこと

  • StackTraceプロパティはその例外の発生とキャッチまでしか保持しないので、その例外だけのStackTraceをログに出すのは不十分

  • GetBaseException()メソッドはその例外の根っこだけを知りたいときに利用する。(基本的にデバッグに用いるもので、ログ出力に用いるべきではなさそう)

ToString()メソッドはinnerExceptionを再帰的に呼び出すので十分な情報が得られる。

参考

  • 【C#】例外チェインからスタックトレースなどの動きを追う
5
5
0

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
5
5