try-catchで例外を発生させる

try-catchでわざと例外を発生させて、キャッチしてcatchロジックに移動する構文です。
Errorオブジェクトに引数を指定することにより任意のエラー番号、エラー説明を設定することができます。
以下、例です。アラートは表示されません。




ソース
<html>
<head>
<title></title>
</head>
<body>
<script language="JavaScript">
<!--
function a(){
    try{
        throw new Error(-500,"エラーエラー");
        alert("アラート");
    }catch(e){
        alert(e.number);
        alert(e.description);
    }
}
// -->
</script>
<input type="button" value="submit" onclick="a()">
</body></html>

初版2008/11/18 :最終更新2008/11/18
HOME