2013年3月10日日曜日

開発環境

『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-87311-425-5) の13章(カスタムオブジェクトと例外の処理)練習問第13-3.を解いてみる。

その他参考書籍

3.

コード(BBEdit)

function checkNum( n ) {
    if ( typeof n !== "number" ) {
        throw { type: "type error",
                message: "NaN"
              };
    }
    return n + ": " + "Number";
}
var result = "";
try {
    result += checkNum( "JavaScript" ) + "\n";
} catch(e) {
    result += e.type + ", " + e.message + "\n";
}
result += checkNum( 10 );
$('#pre0').text(result);



ちなみにPython3.3の場合。

コード(BBEdit)

sample.py

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

def checkNum( n ):
    if type(n) != type(10) and type(n) != type(1.2):
        raise Exception("NaN")
    return "Number"

try:
    print(checkNum("python"))
except Exception as err:
    print(err)

print(checkNum(100))
print(checkNum(12345.6789))

入出力結果(Terminal)

$ ./sample.py
NaN
Number
Number
$

0 コメント:

コメントを投稿