2013年3月14日木曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第16章(例外)16.12(練習問題)練習16-2.を解いてみる。

その他参考書籍

練習16-2.

コード

using System;
using System.Collections.Generic;

class Cat
{
    private int age;
    public Cat(int age)
    {
        this.age = age;
    }
    public int Age
    {
        get { return age; }
    }
}

class CustomCatError : Exception
{
    public CustomCatError(string msg) :
        base(msg) { }
}

class Tester
{
    public void CatManager(Cat cat)
    {
        if (cat.Age <= 0)
        {
            throw new CustomCatError("猫の年齢が0以下になっています!");
        }
        Console.WriteLine(cat.Age);
    }
    public void Run()
    {
        try
        {
            Console.WriteLine("Catオブジェクトの割り当て開始");
            Cat tama = new Cat(5);
            Cat sora = new Cat(-5);
            List<Cat> cats = new List<Cat>();
            cats.Add(tama);
            cats.Add(sora);
            foreach (Cat item in cats)
            {
                CatManager(item);
            }
            Console.WriteLine("tryブロック終了");
        }
        catch (CustomCatError e)
        {
            Console.WriteLine(e.Message);
        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            Console.WriteLine("Catオブジェクトの割り当て解除");
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

Catオブジェクトの割り当て開始
5
猫の年齢が0以下になっています!
Catオブジェクトの割り当て解除
続行するには何かキーを押してください . . .

ちなみにJavaScriptの場合。

コード(BBEdit)

try{
    var Cat = function ( age ) {
        var age = age;
        this.getAge = function( ) {
            return age;
        };
    },
        catManager = function ( cat ) {
            if (cat.getAge() <= 0 ) {
                throw {
                    type: "カスタム例外",
                    message: "猫の年齢が0以下になっています!",
                };
            }
            return cat.getAge();
        },
        tama = new Cat(5),
        sora = new Cat(-5),
        cats = [tama, sora],
        result = "",
        i, max;
    for (i = 0, max = cats.length; i < max; i += 1) {
        result += catManager( cats[i] ) + "\n";
    }
    result += "tryブロック終了\n";
} catch (e) {
    result += e.type + ": " + e.message + "\n";
} finally {
    $('#pre0').text(result);
}



pythonの場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

class Cat:
    def __init__(self, age):
        self._age = age
    def getAge(self):
        return self._age
def catManager(cat):
    if cat.getAge() <= 0:
        raise Exception("猫の年齢が0以下になっています!")
    print(cat.getAge())
try:
    tama = Cat(5)
    sora = Cat(-5)
    cats = [tama, sora]
    for cat in cats:
        catManager(cat)
    print("tryブロック終了")

except Exception as err:
    print(type(err))
    print(err.args)
    print(err)
finally:
    print("Catオブジェクトの割り当て解除")

入出力結果(Terminal)

$ ./sample.py
5
<class 'Exception'>
('猫の年齢が0以下になっています!',)
猫の年齢が0以下になっています!
Catオブジェクトの割り当て解除
$

0 コメント:

コメントを投稿