2018年8月9日木曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の8章(例外処理 - 消化は時代遅れ)、例外マグネット(p. 365)を取り組んでみる。

コード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("when it ");
            ExTestDrive.Zero("yes");
            Console.Write(" it ");
            ExTestDrive.Zero("no");
            Console.WriteLine(".");
        }
    }
    public class MyException : Exception { }
    public class ExTestDrive
    {
        public static void Zero(string test)
        {
            Console.Write("t");
            try
            {
                doRisky(test);
                Console.Write("o");
            }
            catch (MyException)
            {
                Console.Write("a");
            }
            finally
            {
                Console.Write("w");
                Console.Write("s");
            }
        }
        private static void doRisky(string t)
        {
            Console.Write("h");
            if ("yes".Equals(t))
            {
                throw new MyException();
            }
            Console.Write("r");
        }
    }
}

入出力結果(コマンドプロンプト)

when it thaws it throws.
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿