JavaScript ကြိုးစား...catch...နောက်ဆုံးမှာ
ဥပမာ
ဤဥပမာသည် try block တွင် typo တစ်ခုရှိသည် ။ သတိပေးချက်သည် စာလုံးပေါင်းမှားနေသည်။
catch block သည် error ကိုဖမ်းပြီး ၎င်းကိုကိုင်တွယ်ရန် code ကို execute လုပ်သည်-
<p id="demo"></p>
<script>
try {
adddlert("Welcome guest!");
}
catch(err) {
document.getElementById("demo").innerHTML = err.message;
}
</script>
အောက်တွင် နောက်ထပ် ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
အမှားအယွင်း တစ်ခု ဖြစ်ပေါ်သော အခါ ၊ JavaScript သည် ရပ်တန့်ပြီး အမှားအယွင်း မက်ဆေ့ချ်တစ်ခုကို ထုတ်ပေးမည်ဖြစ်သည်။
မှတ်ချက်
၎င်းအတွက် နည်းပညာဆိုင်ရာ အသုံးအနှုန်းမှာ- JavaScript သည် ချွင်းချက် တစ်ခုဖြစ်သည်။
JavaScript သည် အမည် နှင့် မက်ဆေ့ဂျ ်ကို ဂုဏ်သတ္တိနှစ်ခုဖြင့် ဖန်တီး ပေးသည် ။
ထုတ်ပြန်ချက် try...catch...finally
ပေါင်းစည်းမှုသည် JavaScript ကို မရပ်တန့်ဘဲ အမှားများကို ကိုင်တွယ်သည်။
အဆိုပါ try
ထုတ်ပြန်ချက်သည် run (ကြိုးစားရန်) ကုဒ်ပိတ်ဆို့ခြင်းကိုသတ်မှတ်သည်။
ထုတ်ပြန်ချက် catch
သည် အမှားအယွင်းမှန်သမျှကို ကိုင်တွယ်ရန် ကုဒ်ပိတ်ဆို့ခြင်းကို သတ်မှတ်သည်။
ထုတ်ပြန်ချက် finally
သည် ရလဒ်မည်သို့ပင်ဖြစ်စေ လုပ်ဆောင်ရန် ကုဒ်ပိတ်ဆို့ခြင်းကို သတ်မှတ်သည်။
ထုတ်ပြန်ချက်တွင် throw
စိတ်ကြိုက်အမှားကို သတ်မှတ်သည်။
နှစ်မျိုးလုံး catch
နှင့် finally
ရွေးချယ်နိုင်သော်လည်း ၎င်းတို့ထဲမှ တစ်ခုကို အသုံးပြုရပါမည်။
မှတ်ချက်
throw with try and catch ကိုအသုံးပြုခြင်းဖြင့် program flow ကို ထိန်းချုပ်နိုင်ပြီး စိတ်ကြိုက် error message များကို ဖန်တီးနိုင်စေပါသည်။
ကိုလည်းကြည့်ပါ-
အထားအသို
try {
tryCode - Code block to run
}
catch(err) {
catchCode -
Code block to handle errors
}
finally {
finallyCode - Code block to be executed regardless of the try result
}
ကန့်သတ်ချက်များ
Parameter | Description |
tryCode | Required. Code block to be tested while executing. |
err | A local reference to the error object. |
catchCode | Optional. Code block to execute if an error occurs. |
finallyCode | Optional. Code block to execute regardless of the try result |
နောက်ထပ် ဥပမာများ
ဤဥပမာသည် ထည့်သွင်းမှုကို ဆန်းစစ်သည်။
တန်ဖိုး မှားပါက ခြွင်းချက် (err) ကို ချလိုက်သည်-
<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() {
const message =
document.getElementById("message");
message.innerHTML = "";
let 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>
နောက်ဆုံး ထုတ်ပြန်ချက်သည် စမ်းသပ်မှုရလဒ်ကို မည်သို့ပင်ဖြစ်စေပြီးနောက် ကုဒ်ကို လုပ်ဆောင်သည် -
function myFunction()
const message =
document.getElementById("message");
message.innerHTML = "";
let 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 = "";
}
}
Browser ပံ့ပိုးမှု
try...catch
ECMAScript3 (ES3) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES3 (JavaScript 1999) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝ ပံ့ပိုးထားသည်။
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |