JavaScript try/catch/finally statement

Pamamaraan at Paggamit

Ang try/catch/finally na statement ay nangangasiwa sa mga posibleng error na maaaring mangyari sa bloke ng kodigo, habang patuloy na tumatakbo ang kodigo.

Ang error ay maaaring sanhi ng encoding error ng programmer, error sa input na nagdudulot ng error, at iba pang hindi inaasahang sitwasyon.

Ang try na statement ay nagbibigay ng kapahintulutan upang magtayo ng isang bloke ng kodigo na dapat pagsubokin kapag nakikita ang error.

Kapag may error sa bloke ng try, ang statement ng catch ay nagbibigay dagdag na gumawa ng bloke ng kodigo na dapat ipatupad.

Ang statement ng finally ay nagbibigay dagdag na magpatupad ng kodigo pagkatapos ng try at catch, kahit anong resulta.

Komentaryo:Ang mga statement ng catch at finally ay optional, ngunit kailangan gamitin isa sa kanila kapag ginagamit ang try statement (hindi kapag ginagamit parehong oras).

Mga payo:Kapag may error, ang JavaScript ay karaniwang sumasabit at gumagawa ng mensahe ng error. Gamitin ang throw statement upang gumawa ng custom error (tumawag sa exception). Kapag ginamit ang throw kasama ang try at catch, maaari mong kontrolin ang fluwido ng programa at gumawa ng custom error message.

Para sa mas maraming kaalaman tungkol sa mga error ng JavaScript, matututunan ang tutorial ng error ng JavaScript.

Example

Sa halimbawa na ito, nagkamali kami ng pagpaliwanag ng mga titik sa kodigo (sa bloke ng try).

Ang halimbawa na ito ay dapat magpakita ng "Welcome guest!" ngunit nagkaroon ng pagkakamali sa pagpaliwanag ng alert.

Ang bloke ng catch ay maaaring hawakan ang mga error at ipatupad ang kodigo upang maiproseso ito:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
try {
  adddlert("Welcome guest!");
}
catch(err) {
  document.getElementById("demo").innerHTML = err.message;
}
</script>
</body>
</html>

Try it yourself

Mayroong mas maraming TIY example sa ibaba ng pahina.

Syntax

try {
  tryCode - Bloke ng kodigo na dapat subukin
}
catch(err) {
  catchCode - Bloke ng kodigo na gagamitin upang mahawakan ang mga error
} 
finally {
  finallyCode - Bloke ng kodigo na dapat ipatupad kahit anong resulta ng try / catch
}

Parameter Value

Parameter Description
tryCode Mandahil. Ang bloke ng kodigo na dapat pagsusuri kapag nagpapatupad.
err Mandahil, kapag ginamit ang catch. Tukoy ang lokal na variable na may reference sa error. Ang variable na ito ay maaaring tumutukoy sa Error object (na naglalaman ng impormasyon tungkol sa naganap na error, gaya ng mensahe "'addlert' is not defined"). Kapag ang exception ay ginawa ng throw statement, ang variable na ito ay tumutukoy sa object na tinukoy ng throw statement (tingnan ang "More Examples").
catchCode Optional. Ang bloke ng kodigo na dapat ipatupad kapag mayroong error sa bloke ng try. Kung walang error, hindi na ito gagawin ang bloke ng kodigo.
finallyCode Optional. Ang bloke ng kodigo na dapat ipatupad kahit anong resulta ng try / catch.

Detalye ng Teknolohiya

JavaScript version: ECMAScript 3

More examples

Example

This example checks the input. If the value is incorrect, it throws an exception (err).

The catch statement catches exceptions (err) and displays a custom error message:

<!DOCTYPE html>
<html>
<body>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">Test Input</button>
<p id="message"></p>
<script>
function myFunction() {
  var message, x;
  message = document.getElementById("message");
  message.innerHTML = "";
  x = document.getElementById("demo").value;
  try { 
    if(x == "") throw "is Empty";
    if(isNaN(x)) throw "not a number";
    if(x > 10) throw "too high";
    if(x < 5) throw "too low";
  }
  catch(err) {
    message.innerHTML = "Input " + err;
  }
}
</script>
</body>
</html>

Try it yourself

Example

The finally statement allows you to execute code after try and catch, regardless of the result:

function myFunction()
  var message, x;
  message = document.getElementById("message");
  message.innerHTML = "";
  x = document.getElementById("demo").value;
  try { 
    if(x == "") throw "Empty";
    if(isNaN(x)) throw "Not a number";
    if(x > 10) throw "Too high";
    if(x < 5) throw "Too low";
  }
  catch(err) {
    message.innerHTML = "Error: " + err + ".";
  }
  finally {
    document.getElementById("demo").value = "";
  }
}

Try it yourself

Browser supports

语句 Chrome IE Firefox Safari Opera
try/catch/finally Support Support Support Support Support

Related Pages

JavaScript Tutorial:JavaScript Error

JavaScript Reference Manual:JavaScript throw Palitaw