π» Programming/Javascript
[Javascript / μλ°μ€ν¬λ¦½νΈ] κ°μ’ #17 - Error Handling ( μμΈ μ²λ¦¬ )
νλ‘κ·Έλλ°μλ μΈκ°μ§ μ’ λ₯μ μλ¬κ° μμ΅λλ€.
(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() |
μ΄μ 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