ํ๋ก๊ทธ๋๋ฐ์๋ ์ธ๊ฐ์ง ์ข
๋ฅ์ ์๋ฌ๊ฐ ์์ต๋๋ค.
(a) ๋ฌธ๋ฒ์๋ฌ (b) ๋ฐํ์ ์๋ฌ (c) ๋
ผ๋ฆฌ์ ์๋ฌ
๋ฌธ๋ฒ ์๋ฌ
๋ฌธ๋ฒ์๋ฌ๋ ํ์ฑ์๋ฌ๋ผ๊ณ ๋ ํฉ๋๋ค. ๋ฌธ๋ฒ์ ๋ง์ง ์์์ ๋ฐ์ํ๋ ์๋ฌ์ฃ . ๋ฌธ๋ฒ์๋ฌ๋ ์ปดํ์ผ์์ ์๋ฌ๊ฐ ์๋ค๊ณ ์๋ ค์ฃผ๊ธฐ๋๋ฌธ์ ์ฐพ๊ธฐ์ฌ์ด ์๋ฌ์
๋๋ค.
์๋ ์์ ๋ ๋ซ๋ ๊ดํธ๋ฅผ ๋นผ๋จน์ ๋ฌธ๋ฒ์๋ฌ์
๋๋ค.
<script type="text/javascript">
<!--
window.print(;
//-->
</script>
|
๋ฐํ์ ์๋ฌ
๋ฐํ์์๋ฌ๋ ์์ธ๋ผ๊ณ ๋ ํ๋ฉฐ ์คํ์์ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. ์๋ ์์ ๋ ๋ฌธ๋ฒ์ ์ด๊ธ๋์ง ์์ง๋ง ์คํ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์๋ํ๋ฉด ์กด์ฌํ์ง ์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ค๊ณ ํ๊ธฐ ๋๋ฌธ์ด์ฃ .
<script type="text/javascript">
<!--
window.printme();
//-->
</script>
|
๋
ผ๋ฆฌ์ ์๋ฌ
๋
ผ๋ฆฌ์ ์ผ๋ก ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒ์ ์ฐพ๊ธฐ๊ฐ ํ๋ ์๋ฌ์
๋๋ค. ๋ฌธ๋ฒ์ ์ผ๋ก๋ ๋ง๊ณ ์คํ์์ ์๋ฌด๋ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ง ์์ง๋ง ์ํ๋ ๊ฐ์ด ์๋์ค๋ ๊ทธ๋ฐ ์๋ฌ์ด๊ธฐ ๋๋ฌธ์ ๋ด๊ฐ ๊ตฌํํ ๋ก์ง์ด ์๋ชป๋์ด์ ๋ฐ์ํ๋ ์๋ฌ๋ผ๊ณ ๋ณด์๋ฉด ๋ฉ๋๋ค.
try...catch...finally
์๋ฐ์์ ์ฐ๋ try-catch์ ์ ์๋ฐ์คํฌ๋ฆฝํธ์์๋ ์ธ ์๊ฐ ์๋ค์
<script type="text/javascript">
<!--
try {
// Code to run
[break;]
} catch ( e ) {
// Code to run if an exception occurs
[break;]
}[ finally {
// Code that is always executed regardless of
// an exception occurring
}]
//-->
</script>
|
์์ :
์กด์ฌํ์ง ์๋ ํจ์๋ฅผ ํธ์ถํ ๊ฒฝ์ฐ ์ด๋ป๊ฒ ์๋ฌ๊ฐ ๋ฐ์ํ๋์ง ํ๋ฒ ๋ณผ๊น์? ์๋ฌ๋ ๋ธ๋ผ์ฐ์ ๋ณ๋ก ๋ค๋ฅธ๊ฒ ๋ณด์ผ ์ ์๋ค๋ ๊ฒ์ ๋ช
์ฌํ์ธ์.
<html>
<head>
<script type="text/javascript">
<!--
function myFunc() { var a = 100;
alert("Value of variable a is : " + myFunc2() ); }
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>
</html>
|
์ด์ try...catch ์ ์ ์ด์ฉํด์ ์๋ฌ๋ฅผ ์ก์๋ณด๋๋ก ํ์ฃ .
<html>
<head>
<script type="text/javascript">
<!--
function myFunc()
{
var a = 100;
try {
alert("Value of variable a is : " + myFunc2() );
} catch ( e ) {
alert("Error: " + e.description );
}
}
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>
</html>
|
finally ๋ try-catch์ ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ ์ํ๋ ํญ์ ์คํ์ด ๋๋ ๋ถ๋ถ์
๋๋ค.
<html>
<head>
<script type="text/javascript">
<!--
function myFunc()
{
var a = 100;
try {
alert("Value of variable a is : " + myFunc2() );
}catch ( e ) {
alert("Error: " + e.description );
}finally {
alert("Finally block will always execute!" );
}
}
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>
</html>
|
throw ์
throw์ญ์ ์๋ฐ์ ์๋ ๊ฐ๋
์ด์ฃ . ์์ธ๊ฐ ๋ฐ์ํ์ ๋ ๋ด๊ฐ ์ฒ๋ฆฌ์ํ๊ณ ๋ค๋ฅธ๋
์ํํ
์ฒ๋ฆฌํด๋ฌ๋ผ๊ณ ๋์ง๋ ๋์
๋๋ค. throw์ ์ฌ์ ์ ์๋ฏธ๊ฐ "๋์ง๋ค" ์
๋๋ค. ์๋ ์์ ๋ฅผ ํ๋ฒ ๋ณด์ฃ .
<html>
<head>
<script type="text/javascript">
<!--
function myFunc()
{
var a = 100;
var b = 0;
try{
if ( b == 0 ){
throw( "Divide by zero error." );
}else{
var c = a / b;
}
}catch ( e ) {
alert("Error: " + e );
}
}
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>
</html>
|
b์ 0์ ํ ๋นํ๊ณ b๊ฐ 0์ด๋ฉด throw ํ๋๋ก ๋ง๋ ์์ ๋ค์. throw๊ฐ ์ด๋ค ์์ผ๋ก ํ๋ฉด์ ํํ๋๋์ง ํ๋ฒ ์คํํด๋ณด์ธ์.
onerror() ๋ฉ์๋
onerror ๋ฉ์๋๋ JavaScript์์ ์๋ฌํธ๋ค๋ง์ ์ํด์ ์ ์ผ ์ฒ์ ๋์จ ๊ธฐ๋ฅ์ด๋ผ๊ณ ํฉ๋๋ค. error ์ด๋ฒคํธ๋ ์นํ์ด์ง์์ ์๋ฌ๊ฐ ๋ฐ์ํ์๋ window๊ฐ์ฒด์์ ๋ฐ์์ํต๋๋ค. ์๋ ์์ ๋ฅผ ํ๋ฒ ๋ณผ๊น์.
<html>
<head>
<script type="text/javascript">
<!--
window.onerror = function () {
alert("An error occurred.");
}
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>
</html>
|
์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ๊ฒฝ๊ณ ์ฐฝ์ ๋์ฐ๋๋ก ํด๋๊ณ ์กด์ฌํ์ง ์๋ ํจ์๋ฅผ ํธ์ถํ๋ ์์ ๊ตฐ์.
ํ๋ฒ ์คํํด๋ณด์ธ์.
onerror ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ ์ธ๊ฐ์ง ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค.
์๋ฌ๋ฉ์์ง . ๋ธ๋ผ์ฐ์ ๊ฐ ๋ณด์ฌ์ฃผ๋ ์๋ฌ๋ฉ์์ง์
๋๋ค.
URL . ์๋ฌ๊ฐ ๋ฐ์ํ ํ์ผ์ ์๋ ค์ค๋๋ค.
๋ผ์ธ ๋๋ฒ . ์๋ฌ๋ฅผ ๋ฐ์์ํจ ๋ผ์ธ์ด ๋ช๋ฒ์งธ ๋ผ์ธ์ธ์ง๋ฅผ ์๋ ค์ค๋๋ค.
์ด ์ธ๊ฐ์ง ์ ๋ณด๋ฅผ ์ด๋ป๊ฒ๋ฐ์์ค๋์ง ํ๋ฒ ๋ณผ๊น์?
<html>
<head>
<script type="text/javascript">
<!--
window.onerror = function (msg, url, line) {
alert("Message : " + msg );
alert("url : " + url );
alert("Line number : " + line );
}
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type="button" value="Click Me" onclick="myFunc();" />
</form>
</body>
</html>
|
onerror ๋ฉ์๋๋ ์๋์๊ฐ์ด ์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ฌ๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด์ ์ฌ์ฉ๋ ์๋ ์์ต๋๋ค.
<img src="myimage.gif"
onerror="alert('An error occurred loading the image.')" />
|
์ด onerror๋ฉ์๋๋ ์ด๋ฏธ์ง ํ๊ทธ ์ด์ธ์๋ ๋ค๋ฅธ ํ๊ทธ์์๋ ์ฌ์ฉ๋ ์ ์๋ค๋ ๊ฒ์ ์์๋์๋ฉด ์ข์ ๋ฏ ์ถ๋ค์.
Reference : http://www.tutorialspoint.com/javascript/javascript_error_handling.htm