2018年6月10日日曜日

開発環境

初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)日向 俊二 (翻訳)、オライリージャパン)の16章(例外)、16.12(練習問題)、練習16-1.を取り組んでみる。

コード

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            List<Cat> cats = new List<Cat>();

            Console.WriteLine(cats[10]);
        }
        catch (ArgumentOutOfRangeException e)
        {
            Console.WriteLine(e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            Console.WriteLine("Catオブジェクトの割り当ての解除");
        }
    }
}

public class Cat
{
    private int age;

    public Cat(int age)
    {
        this.age = age;
    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }
}

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

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Catオブジェクトの割り当ての解除
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿