ﺏﺎﺗﺮﭘ، ﻭ ﺖﯾﺎﻬﻧ ﺭﺩ...ﺪﯾﺮﯿﮕﺑ...ﺪﯿﻨﮐ ﯽﻌﺳ.ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ (ﻥﺎﺤﺘﻣﺍ ﯼﺍﺮﺑ) ﺍﺮﺟﺍ ﯼﺍﺮﺑ ﺍﺭ ﺪﮐ ﮎﻮﻠﺑ ﮏﯾ try
.ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺎﻄﺧ ﻪﻧﻮﮔﺮﻫ ﻪﺑ ﯽﮔﺪﯿﺳﺭ ﯼﺍﺮﺑ ﺍﺭ ﺪﮐ ﮎﻮﻠﺑ ﮏﯾ catch
.ﺩﻮﺷ ﺍﺮﺟﺍ ﻪﺠﯿﺘﻧ ﻪﺑ ﻪﺟﻮﺗ ﻥﻭﺪﺑ ﺎﺗ ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﺪﮐ ﮎﻮﻠﺑ ﮏﯾ ﺖﯾﺎﻬﻧ ﺭﺩ<"code clas
.ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﯽﺷﺭﺎﻔﺳ ﯼﺎﻄﺧ ﮏﯾ throw ﺕﺭﺎﺒﻋ
ﺖﺳﺍ ﻦﮑﻤﻣ ﯽﻔﻠﺘﺨﻣ ﯼﺎﻫﺎﻄﺧ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺪﮐ ﯼﺍﺮﺟﺍ ﻡﺎﮕﻨﻫ
ﺪﺷﺎﺑ ﻩﺎﺒﺘﺷﺍ ﺯﺍ ﯽﺷﺎﻧ ﯼﺎﻫﺎﻄﺧ ،ﺲﯾﻮﻧ ﻪﻣﺎﻧﺮﺑ ﻂﺳﻮﺗ ﻩﺪﺷ ﺩﺎﺠﯾﺍ ﯽﺴﯾﻮﻧﺪﮐ ﯼﺎﻫﺎﻄﺧ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺎﻫﺎﻄﺧ
:ﻢﯿﻨﮐ ﺩﺎﺠﯾﺍ ﺎﻄﺧ ﮏﯾ ﺍًﺪﻤﻋ ﺎﺗ ﻢﯿﺘﺷﻮﻧ ﻩﺎﺒﺘﺷﺍ "adddlert" ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ "ﺭﺍﺪﺸﻫ" ﺎﻣ ﻝﺎﺜﻣ ﻦﯾﺍ ﺭ
<p id="demo"></p>
<script>
try {
adddlert("Welcome guest!");
}
catch(err) {
document.getElementById("demo").innerHTML = err.message;
}
</script>→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Handling</h2>
<p>How to use <b>catch</b> to display an error.</p>
<p id="demo"></p>
<script>
try {
adddlert("Welcome guest!");
}
catch(err) {
document.getElementById("demo").innerHTML = err.message;
}
</script>
</body>
</html>
ﺪﻨﮐ ﯽﻣ ﺍﺮﺟﺍ ﺍﺭ ﻥﺁ ﻭ ﺪﻨﮐ ﯽﻣ ﺖﻓﺎﯾﺭﺩ ﺎﻄﺧ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ adddlert ﺖﭙﯾﺮﮑﺳ
try ... catch
.ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ ﺪﮐ ﺯﺍ ﯽﮐﻮﻠﺑ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ try ﺕ
ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ ﺪﮐ ﺯﺍ ﯽﮐﻮﻠﺑ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ catch
catch ﻭ try ﺖﭙﯾﺮﮑﺳﺍ
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}ﺪﻫﺩ ﯽﻣ ﻡﺎﺠﻧﺍ ﺍﺭ ﺭﺎﮐ ﻦﯾﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ﺪﻫﺩ ﯽﻣ ﺥﺭ ﯽﯾﺎﻄﺧ ﻪﮐ ﯽﻣﺎﮕﻨﻫ
ﺩﺮﮐ ﺪﻫﺍﻮﺧ ﺏﺎﺗﺮﭘ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ :ﺖﺳﺍ ﻦﯾﺍ ﯼﺍﺮﺑ ﯽﻨﻓ ﺡﻼﻄﺻﺍ
:ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ ﯽﮔﮋﯾﻭ ﻭﺩ ﺎﺑ ﺎﻄﺧ ءﯽﺷ ﮏﯾ ﻊﻗﺍﻭ ﺭﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ
ﺏﺎﺗﺮﭘ ﻪﯿﻧﺎﯿﺑ.ﺪﯿﻨﮐ ﺩﺎﺠﯾﺍ ﯽﺷﺭﺎﻔﺳ ﯼﺎﻄﺧ ﮏﯾ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ throw
.ﺪﯿﻨﮐ ﺩﺎﺠﯾﺍ (ﺎﻄﺧ ﮏﯾ ﺏﺎﺗﺮﭘ) ﺎﻨﺜﺘﺳﺍ ﮏﯾ ﺪﯿﻧﺍﻮﺗﯽﻣ ،ﯽﻨﻓ ﺮﻈﻧ ﺯﺍ
:ءﯽﺷ<"code class="w3-codespan> ﺎﯾ ﯽﻟﻮﺑ .ﺪﺷﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ <"code class=
throw "Too big"; // throw a text
throw 500; // throw a numberﻭ ﺪﯿﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ<"code class="w3-codespan> ﺎﺑ ﻩﺍﺮﻤﻫ ﺏﺎﺗﺮﭘ<"code class=
،ﺖﺳﺍ ﻩﺎﺒﺘﺷﺍ ﺭﺍﺪﻘﻣ ﺮﮔﺍ .ﺪﻨﮐ ﯽﻣ ﯽﺳﺭﺮﺑ ﺍﺭ ﯼﺩﻭﺭﻭ ﻝﺎﺜﻣ ﻦﯾﺍ
:ﺩﻮﺷ ﯽﻣ ﻩﺩﺍﺩ ﺶﯾﺎﻤﻧ ﯽﺷﺭﺎﻔﺳ ﯼﺎﻄﺧ ﻡﺎﯿﭘ ﮏﯾ ﻭ ﺩﻮﺷ ﯽﻣ ﻪﺘﻓﺮﮔ catch ﺭﻮﺘﺳﺩ ﻂﺳﻮﺗ (err) ﺎﻨﺜﺘﺳﺍ
<!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="p01"></p>
<script>
function myFunction() {
const message = document.getElementById("p01");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x.trim() == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw
"too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript try catch</h2>
<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="p01"></p>
<script>
function myFunction() {
const message = document.getElementById("p01");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x.trim() == "") throw "empty";
if(isNaN(x)) throw "not a number";
x = Number(x);
if(x < 5) throw "too low";
if(x > 10) throw "too high";
}
catch(err) {
message.innerHTML = "Input is " + err;
}
}
</script>
</body>
</html>
.ﺖﺳﺍ ﻝﺎﺜﻣ ﮏﯾ ﻂﻘﻓ ﻻﺎﺑ ﺪﮐ
ﺪﻨﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﯽﻠﺧﺍﺩ HTML ﻭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺯﺍ ﯽﺒﯿﮐﺮﺗ ﺯﺍ ﺐﻠﻏﺍ ﻥﺭﺪﻣ ﯼﺎﻫﺮﮔﺭﻭﺮﻣ
<input id="demo" type="number" min="5" max="10" step="1">.ﺪﯿﻧﺍﻮﺨﺑ ﺎﻫ ﻡﺮﻓ ﯽﺠﻨﺳ ﺭﺎﺒﺘﻋﺍ ﺩﺭﻮﻣ ﺭﺩ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﯼﺪﻌﺑ ﻞﺼﻓ ﺭﺩ
ﺖﯾﺎﻬﻧ ﺭﺩ ﻪﯿﻧﺎﯿﺑﻭ ﻥﺎﺤﺘﻣﺍ ﺯﺍ ﺲﭘ ﺍﺭ ﺪﮐ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ ﺖﯾﺎﻬﻧ ﺭﺩ<"code class="w3-codespan> ﺕﺭ
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
finally {
Block of code to be executed regardless of the try / catch result
}function myFunction() {
const message = document.getElementById("p01");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x.trim() == "") throw "is empty";
if(isNaN(x))
throw "is not a number";
x = Number(x);
if(x >
10) throw "is too high";
if(x < 5) throw "is too low";
}
catch(err)
{
message.innerHTML = "Error: " +
err + ".";
}
finally {
document.getElementById("demo").value = "";
}
}→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript try catch</h2>
<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="p01"></p>
<script>
function myFunction() {
const message = document.getElementById("p01");
message.innerHTML = "";
let x = document.getElementById("demo").value;
try {
if(x.trim() == "") throw "is empty";
if(isNaN(x)) throw "is not a number";
x = Number(x);
if(x > 10) throw "is too high";
if(x < 5) throw "is too low";
}
catch(err) {
message.innerHTML = "Input " + err;
}
finally {
document.getElementById("demo").value = "";
}
}
</script>
</body>
</html>
ﺪﻫﺩ ﯽﻣ ﻪﺋﺍﺭﺍ ﺍﺭ ﺎﻄﺧ ﺕﺎﻋﻼﻃﺍ ﻪﮐ ﺖﺳﺍ ﯽﻠﺧﺍﺩ ﯼﺎﻄﺧ ءﯽﺷ ﮏﯾ ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ
.ﻡﺎﯿﭘ ﻭ ﻡﺎﻧ :ﺪﻫﺩ ﯽﻣ ﻪﺋﺍﺭﺍ ﺍﺭ ﺪﯿﻔﻣ ﯽﮔﮋﯾﻭ ﻭﺩ ﺎﻄﺧ ﯽﺷ
ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺎﯾ ﻢﯿﻈﻨﺗ ﺍﺭ ﺎﻄﺧ ﻡﺎﻧ
ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺎﯾ ﻢﯿﻈﻨﺗ ﺍﺭ (ﻪﺘﺷﺭ) ﺎﻄﺧ ﻡﺎﻐﯿﭘ
:ﺪﻧﺍﺩﺮﮔﺮﺑ ﺎﻄﺧ ﻡﺎﻧ ﯽﮔﮋﯾﻭ ﺎﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﻒﻠﺘﺨﻣ ﺭﺍﺪﻘﻣ ﺶﺷ
ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ eval() ﻊﺑﺎﺗ ﺭﺩ ﯽﯾﺎﻄﺧ
ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ "ﻩﺩﻭﺪﺤﻣ ﺯﺍ ﺝﺭﺎﺧ" ﺩﺪﻋ ﮏﯾ
ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ ﯽﻧﻮﻧﺎﻗﺮﯿﻏ ﻊﺟﺮﻣ ﮏﯾ
ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ ﯼﻮﺤﻧ ﯼﺎﻄﺧ ﮏﯾ
ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ ﻉﻮﻧ ﯼﺎﻄﺧ ﮏﯾ
ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ()encodeURI ﺭﺩ ﯽﯾﺎﻄﺧ
.ﺖﺳﺍ ﻩﺪﺷ ﻩﺩﺍﺩ ﺢﯿﺿﻮﺗ ﺮﯾﺯ ﺭﺩ ﻒﻠﺘﺨﻣ ﺭﺍﺪﻘﻣ ﺶﺷ
.ﺖﺳﺍ eval() ﻊﺑﺎﺗ ﺭﺩ ﺎﻄﺧ ﮏﯾ ﻩﺪﻨﻫﺩ ﻥﺎﺸﻧ EvalError ﮏﯾ
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ SyntaxError ﺯﺍ ﻥﺁ ﯼﺎﺟ ﻪﺑ .ﺪﻨﻨﮐ ﯽﻤﻧ ﺩﺎﺠﯾﺍ ﺍﺭ EvalError ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺪﯾﺪﺟ
ﺩﻮﺷ ﯽﻣ ﺏﺎﺗﺮﭘ RangeError ،ﺖﺳﺍ ﻥﺁ ﺯﺍ ﺝﺭﺎﺧ ﻪﮐ ﺪﯿﻨﮐ ﻩﺩﺎﻔ
ﺪﯿﻨﮐ ﻢﯿﻈﻨﺗ ﻥﺁ ﯼﻭﺭ ﺍﺭ ﺩﺪﻋ ﮏﯾ ﻢﻬﻣ ﻡﺎﻗﺭﺍ ﺩﺍﺪﻌﺗ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ :ﻝﺎﺜﻣ ﻥﺍﻮﻨﻋ ﻪﺑ
let num = 1;
try {
num.toPrecision(500); // A number cannot have 500 significant digits
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Errors</h2>
<p>You cannot set the number of significant digits of a number to 500:</p>
<p id="demo">
<script>
let num = 1;
try {
num.toPrecision(500);
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
ﺩﻮﺷ ﯽﻣ ﺏﺎﺗﺮﭘ ﻊﺟﺮﻣ ﯼﺎﻄﺧ<"code class="w3-codespan> ﮏﯾ ،ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ (ﻊﺟﺮﻣ) ﺮﯿﻐﺘﻣ
let x = 5;
try {
x = y + 1; // y cannot be used (referenced)
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Errors</h2>
<p>You cannot use the value of a non-existing variable:</p>
<p id="demo"></p>
<script>
let x = 5;
try {
x = y + 1;
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
ﺎﺑ ﺍﺭ ﺪﮐ ﺪﯿﻫﺍﻮﺨﺑ ﺮﮔﺍ
try {
eval("alert('Hello)"); // Missing ' will produce an error
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
} → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Errors</h2>
<p>You cannot evaluate code that contains a syntax error:</p>
<p id="demo"></p>
<script>
try {
eval("alert('Hello)");
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
ﺯﺍ ﺝﺭﺎﺧ ﯼﺭﺍﺪﻘﻣ ﺯﺍ ﺮﮔﺍ
let num = 1;
try {
num.toUpperCase(); // You cannot convert a number to upper case
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Errors</h2>
<p>You cannot convert a number to upper case:</p>
<p id="demo"></p>
<script>
let num = 1;
try {
num.toUpperCase();
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
:ﺩﻮﺷﯽﻣ ﺩﺎﺠﯾﺍ
try {
decodeURI("%%%"); // You cannot URI decode percent signs
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Errors</h2>
<p>Some characters cannot be decoded with decodeURI():</p>
<p id="demo"></p>
<script>
try {
decodeURI("%%%");
}
catch(err) {
document.getElementById("demo").innerHTML = err.name;
}
</script>
</body>
</html>
:ﺪﻨﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﺩﺭﺍﺪﻧﺎﺘﺳﺍ ﺮﯿﻏ ﯼﺎﻄﺧ ﯽﺷ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺯﺍ ﯽﺧﺮﺑ ﺖﻓﺎﺳﻭﺮﮑﯾﺎﻣ ﻭ ﻼﯾﺯﻮﻣ
fileName (Mozilla)
lineNumber (Mozilla)
columnNumber (Mozilla)
stack (Mozilla)
description (Microsoft)
number (Microsoft).ﺩﺮﮐ ﺪﻨﻫﺍﻮﺨﻧ ﺭﺎﮐ ﺎﻫﺮﮔﺭﻭﺮﻣ ﻪﻤﻫ ﺭﺩ ﺎﻬﻧﺁ .ﺪﯿﻨﮑﻧ ﻩﺩﺎﻔﺘﺳﺍ ﯽﻣﻮﻤﻋ ﯼﺎﻫ ﺖﯾﺎﺳ ﺏﻭ ﺭﺩ ﺎﻫ ﯽﮔﮋﯾﻭ ﻦﯾ
ﺪﯾﻭﺮﺑ ﺎﻣ Complete ﻪﺑ ،Error ﯽﺷ ﻞﻣﺎﮐ ﻉﺎﺟﺭﺍ ﯼﺍﺮﺑ