ﺏﺎﺗﺮﭘ
، ﻭ ﺖﯾﺎﻬﻧ ﺭﺩ...ﺪﯾﺮﯿﮕﺑ...ﺪﯿﻨﮐ ﯽﻌﺳ
.ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ (ﻥﺎﺤﺘﻣﺍ ﯼﺍﺮﺑ) ﺍﺮﺟﺍ ﯼﺍﺮﺑ ﺍﺭ ﺪﮐ ﮎﻮﻠﺑ ﮏﯾ .ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺎﻄﺧ ﻪﻧﻮﮔﺮﻫ ﻪﺑ ﯽﮔﺪﯿﺳﺭ ﯼﺍﺮﺑ ﺍﺭ ﺪﮐ ﮎﻮﻠﺑ ﮏﯾ .ﺩﻮﺷ ﺍﺮﺟﺍ ﻪﺠﯿﺘﻧ ﻪﺑ ﻪﺟﻮﺗ ﻥﻭﺪﺑ ﺎﺗ ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﺪﮐ ﮎﻮﻠﺑ ﮏﯾ .ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﯽﺷﺭﺎﻔﺳ ﯼﺎﻄﺧ ﮏﯾ ﺖﺳﺍ ﻦﮑﻤﻣ ﯽﻔﻠﺘﺨﻣ ﯼﺎﻫﺎﻄﺧ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺪﮐ ﯼﺍﺮﺟﺍ ﻡﺎﮕﻨﻫ ﺪﺷﺎﺑ ﻩﺎﺒﺘﺷﺍ ﺯﺍ ﯽﺷﺎﻧ ﯼﺎﻫﺎﻄﺧ ،ﺲﯾﻮﻧ ﻪﻣﺎﻧﺮﺑ ﻂﺳﻮﺗ ﻩﺪﺷ ﺩﺎﺠﯾﺍ ﯽﺴﯾﻮﻧﺪﮐ ﯼﺎﻫﺎﻄﺧ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺎﻫﺎﻄﺧ :ﻢﯿﻨﮐ ﺩﺎﺠﯾﺍ ﺎﻄﺧ ﮏﯾ ﺍًﺪﻤﻋ ﺎﺗ ﻢﯿﺘﺷﻮﻧ ﻩﺎﺒﺘﺷﺍ "adddlert" ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ "ﺭﺍﺪﺸﻫ" ﺎﻣ ﻝﺎﺜﻣ ﻦﯾﺍ ﺭ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺪﻨﮐ ﯽﻣ ﺍﺮﺟﺍ ﺍﺭ ﻥﺁ ﻭ ﺪﻨﮐ ﯽﻣ ﺖﻓﺎﯾﺭﺩ ﺎﻄﺧ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ adddlert ﺖﭙﯾﺮﮑﺳ .ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ ﺪﮐ ﺯﺍ ﯽﮐﻮﻠﺑ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ ﺪﮐ ﺯﺍ ﯽﮐﻮﻠﺑ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ ﺪﻫﺩ ﯽﻣ ﻡﺎﺠﻧﺍ ﺍﺭ ﺭﺎﮐ ﻦﯾﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ﺪﻫﺩ ﯽﻣ ﺥﺭ ﯽﯾﺎﻄﺧ ﻪﮐ ﯽﻣﺎﮕﻨﻫ ﺩﺮﮐ ﺪﻫﺍﻮﺧ ﺏﺎﺗﺮﭘ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ :ﺖﺳﺍ ﻦﯾﺍ ﯼﺍﺮﺑ ﯽﻨﻓ ﺡﻼﻄﺻﺍ :ﺪﻨﮐ ﯽﻣ ﺩﺎﺠﯾﺍ ﯽﮔﮋﯾﻭ ﻭﺩ ﺎﺑ ﺎﻄﺧ ءﯽﺷ ﮏﯾ ﻊﻗﺍﻭ ﺭﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ .ﺪﯿﻨﮐ ﺩﺎﺠﯾﺍ ﯽﺷﺭﺎﻔﺳ ﯼﺎﻄﺧ ﮏﯾ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ .ﺪﯿﻨﮐ ﺩﺎﺠﯾﺍ (ﺎﻄﺧ ﮏﯾ ﺏﺎﺗﺮﭘ) ﺎﻨﺜﺘﺳﺍ ﮏﯾ ﺪﯿﻧﺍﻮﺗﯽﻣ ،ﯽﻨﻓ ﺮﻈﻧ ﺯﺍ : ﻭ ،ﺖﺳﺍ ﻩﺎﺒﺘﺷﺍ ﺭﺍﺪﻘﻣ ﺮﮔﺍ .ﺪﻨﮐ ﯽﻣ ﯽﺳﺭﺮﺑ ﺍﺭ ﯼﺩﻭﺭﻭ ﻝﺎﺜﻣ ﻦﯾﺍ :ﺩﻮﺷ ﯽﻣ ﻩﺩﺍﺩ ﺶﯾﺎﻤﻧ ﯽﺷﺭﺎﻔﺳ ﯼﺎﻄﺧ ﻡﺎﯿﭘ ﮏﯾ ﻭ ﺩﻮﺷ ﯽﻣ ﻪﺘﻓﺮﮔ catch ﺭﻮﺘﺳﺩ ﻂﺳﻮﺗ (err) ﺎﻨﺜﺘﺳﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺖﺳﺍ ﻝﺎﺜﻣ ﮏﯾ ﻂﻘﻓ ﻻﺎﺑ ﺪﮐ ﺪﻨﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﯽﻠﺧﺍﺩ HTML ﻭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺯﺍ ﯽﺒﯿﮐﺮﺗ ﺯﺍ ﺐﻠﻏﺍ ﻥﺭﺪﻣ ﯼﺎﻫﺮﮔﺭﻭﺮﻣ .ﺪﯿﻧﺍﻮﺨﺑ ﺎﻫ ﻡﺮﻓ ﯽﺠﻨﺳ ﺭﺎﺒﺘﻋﺍ ﺩﺭﻮﻣ ﺭﺩ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﯼﺪﻌﺑ ﻞﺼﻓ ﺭﺩ ﻭ ﻥﺎﺤﺘﻣﺍ ﺯﺍ ﺲﭘ ﺍﺭ ﺪﮐ ﺪﻫﺩ ﯽﻣ ﻥﺎﮑﻣﺍ ﺎﻤﺷ ﻪﺑ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺪﻫﺩ ﯽﻣ ﻪﺋﺍﺭﺍ ﺍﺭ ﺎﻄﺧ ﺕﺎﻋﻼﻃﺍ ﻪﮐ ﺖﺳﺍ ﯽﻠﺧﺍﺩ ﯼﺎﻄﺧ ءﯽﺷ ﮏﯾ ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ .ﻡﺎﯿﭘ ﻭ ﻡﺎﻧ :ﺪﻫﺩ ﯽﻣ ﻪﺋﺍﺭﺍ ﺍﺭ ﺪﯿﻔﻣ ﯽﮔﮋﯾﻭ ﻭﺩ ﺎﻄﺧ ﯽﺷ ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺎﯾ ﻢﯿﻈﻨﺗ ﺍﺭ ﺎﻄﺧ ﻡﺎﻧ ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺎﯾ ﻢﯿﻈﻨﺗ ﺍﺭ (ﻪﺘﺷﺭ) ﺎﻄﺧ ﻡﺎﻐﯿﭘ :ﺪﻧﺍﺩﺮﮔﺮﺑ ﺎﻄﺧ ﻡﺎﻧ ﯽﮔﮋﯾﻭ ﺎﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﻒﻠﺘﺨﻣ ﺭﺍﺪﻘﻣ ﺶﺷ ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ eval() ﻊﺑﺎﺗ ﺭﺩ ﯽﯾﺎﻄﺧ ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ "ﻩﺩﻭﺪﺤﻣ ﺯﺍ ﺝﺭﺎﺧ" ﺩﺪﻋ ﮏﯾ ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ ﯽﻧﻮﻧﺎﻗﺮﯿﻏ ﻊﺟﺮﻣ ﮏﯾ ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ ﯼﻮﺤﻧ ﯼﺎﻄﺧ ﮏﯾ ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ ﻉﻮﻧ ﯼﺎﻄﺧ ﮏﯾ ﺖﺳﺍ ﻩﺩﺍﺩ ﺥﺭ()encodeURI ﺭﺩ ﯽﯾﺎﻄﺧ .ﺖﺳﺍ ﻩﺪﺷ ﻩﺩﺍﺩ ﺢﯿﺿﻮﺗ ﺮﯾﺯ ﺭﺩ ﻒﻠﺘﺨﻣ ﺭﺍﺪﻘﻣ ﺶﺷ .ﺖﺳﺍ eval() ﻊﺑﺎﺗ ﺭﺩ ﺎﻄﺧ ﮏﯾ ﻩﺪﻨﻫﺩ ﻥﺎﺸﻧ .ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ SyntaxError ﺯﺍ ﻥﺁ ﯼﺎﺟ ﻪﺑ .ﺪﻨﻨﮐ ﯽﻤﻧ ﺩﺎﺠﯾﺍ ﺍﺭ EvalError ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺪﯾﺪﺟ ﺩﻮﺷ ﯽﻣ ﺏﺎﺗﺮﭘ ﺪﯿﻨﮐ ﻢﯿﻈﻨﺗ ﻥﺁ ﯼﻭﺭ ﺍﺭ ﺩﺪﻋ ﮏﯾ ﻢﻬﻣ ﻡﺎﻗﺭﺍ ﺩﺍﺪﻌﺗ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ :ﻝﺎﺜﻣ ﻥﺍﻮﻨﻋ ﻪﺑ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺩﻮﺷ ﯽﻣ ﺏﺎﺗﺮﭘ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺎﺑ ﺍﺭ ﺪﮐ ﺪﯿﻫﺍﻮﺨﺑ ﺮﮔﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﺯﺍ ﺝﺭﺎﺧ ﯼﺭﺍﺪﻘﻣ ﺯﺍ ﺮﮔﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺩﻮﺷﯽﻣ ﺩﺎﺠﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧtry
catch
ﺖﯾﺎﻬﻧ ﺭﺩ<"code clas
throw
ﺕﺭﺎﺒﻋ !ﺩﺎﺘﻓﺍ ﺪﻫﺍﻮﺧ ﻕﺎﻔﺗﺍ ﺎﻫﺎﻄﺧ
ﻝﺎﺜﻣ
<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>
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=
ﯼﺩﻭﺭﻭ ﯽﺠﻨﺳ ﺭﺎﺒﺘﻋﺍ ﻪﻧﻮﻤﻧ
<!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>
ﺎﻄﺧ ءﯽﺷ
ﯽﺷ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺎﻄﺧ
ﻡﺎﻧ ﺮﯾﺩﺎﻘﻣ ﯼﺎﻄﺧ
ﻝﺍﻭﺍ ﯼﺎﻄﺧ
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>
.(ﻥﺎﺴﮑﯾ ﻊﺒﻨﻣ ﻪﺳﺎﻨﺷ) URI ﯼﺎﻄﺧ
ﻝﺎﺜﻣ
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 ﯽﺷ ﻞﻣﺎﮐ ﻉﺎﺟﺭﺍ ﯼﺍﺮﺑ