2009年12月5日土曜日

ランタイム例外ではなくカスタム例外を作成し、throw文を使用してスルーしてみる。
using System;

class CustomException : Exception
{
    // コンストラクタでMessageプロパティに文字列を設定する
    public CustomException(string message)
        : base(message)
    {
    }
}

class MainClass
{
    static void Main()
    {
        try
        {
            // カスタム例外をスロー
            throw new CustomException("例外をスロー");
        }
        catch (CustomException error)
        {
            // 出力値:例外をスロー
            Console.WriteLine(error.Message);
        }
        finally
        {
            // 出力値:終了
            Console.WriteLine("終了");
        }
    }
}

0 コメント:

コメントを投稿