開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第16章(例外)16.12(練習問題)練習16-2を解いてみる。
その他参考書籍
練習16-2.
コード
using System;
using System.Collections.Generic;
class Cat
{
private int age;
private string name;
public Cat(int age, string name)
{
this.age = age;
this.name = name;
}
public int Age
{
get { return age; }
set { age = value; }
}
public override string ToString()
{
return "Name: " + name + ", Age: " + age;
}
}
class CustomCatError : System.Exception
{
public CustomCatError(string message)
: base(message) { }
}
class Tester
{
private void CheckCat(Cat cat)
{
if (cat.Age <= 0)
{
throw new CustomCatError("猫の年齢が0以下になっています!");
}
}
public void CatManager(Cat cat)
{
CheckCat(cat);
Console.WriteLine(cat);
}
public void Run()
{
try
{
Console.WriteLine("Catオブジェクトの割り当て開始");
Cat tama = new Cat(5, "Tama");
Cat sora = new Cat(-5, "Sora");
List<Cat> cats = new List<Cat>();
cats.Add(tama);
cats.Add(sora);
// ループ中に例外発生
foreach (Cat cat in cats)
{
CatManager(cat);
}
Console.WriteLine("猫のリストの出力終了");
}
catch (CustomCatError e)
{
Console.WriteLine(e.Message);
}
catch (ArgumentOutOfRangeException 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オブジェクトの割り当て開始 Name: Tama, Age: 5 猫の年齢が0以下になっています! Catオブジェクトの割り当て解除 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(TextWrangler)
var Cat = function(age, name){
var age;
var name;
this.get_age = function(){
return age;
};
this.set_age = function(value){
age = value;
};
this.to_string = function(){
return "Name: " + name + ", Age: " + age;
};
};
var CustomCatError = function(){};
CustomCatError.prototype = new Error("猫の年齢が0以下になっています!");
CustomCatError.prototype.constructor = CustomCatError;
var result = "";
function check_cat(cat){
if(cat.get_age() <= 0) throw new CustomCatError();
}
function cat_manager(cat){
check_cat(cat);
result += cat.to_string() + "\n";
}
try{
result += "Catオブジェクトの割り当て開始\n";
var tama = new Cat(5, "Tama");
var sora = new Cat(-5, "Sora");
var cats = [tama, sora];
for(var i = 0; i < cats.length; i++){
cat_manager(cats[i]);
}
result += "猫のリストの出力終了\n"
} catch(e){
result += e + "\n";
} finally {
result += "Catオブジェクトの割り当て解除\n";
}
$('#pre0').text(result);
JavaScriptの継承についてはまだいまいち理解できてないことも多かったり。
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class Cat:
def __init__(self,age, name):
self.age = age
self.name = name
def __str__(self):
return "Name: {0}, Age: {1}".format(self.name, self.age)
def get_age(self):
return self.age
def set_age(self,value):
self.age = value
class CustomCatError(Exception):pass
def check_cat(cat):
if cat.get_age() <= 0:
raise CustomCatError()
def cat_manager(cat):
check_cat(cat)
print(cat)
if __name__ == '__main__':
try:
print("Catオブジェクトの割り当て開始")
tama = Cat(5, "Tama")
sora = Cat(-5, "Sora")
cats = [tama, sora]
for cat in cats:
cat_manager(cat)
print("猫のリストの出力終了")
except CustomCatError:
print("エラー: 猫の年齢が0以下になっています!")
except IndexError:
print("エラー: IndexError")
except Exception:
print("エラー: 未知の例外")
finally:
print("Catオブジェクトの割り当て解除")
入出力結果(Terminal)
$ ./sample.py Catオブジェクトの割り当て開始 Name: Tama, Age: 5 エラー: 猫の年齢が0以下になっています! Catオブジェクトの割り当て解除 $
Pythonでのクラス例外、エラーメッセージの使い方がまだいまいち理解できてないかも。
0 コメント:
コメントを投稿