例外処理
概要
try-catch-finally
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>テストサイト</title>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
try{
// 継続条件
for(var i = 0;i< 10;i++){
if(i >= 9){
throw "エラー!";
}
// 余りを取得
var data = i % 2;
// 0の場合は処理しない
if(data == 0){
continue;
}
document.write("i=" + i + "<br />");
}
document.write("<br />");
}catch(e){
document.write("★" + e + "★<br />");
}finally{
// 出力1
document.write("Loop End" + "<br />");
}
// 出力1
document.write("Test Javascript1" + "<br />");
end1 :
// 出力2
document.write("Test Javascript2" + "<br />");
//]]>
</script>
</body>
</html>
最終更新:2012年02月15日 22:16