PHP ခြွင်းချက်များ
ခြွင်းချက်ဆိုတာဘာလဲ။
ခြွင်းချက်တစ်ခုသည် PHP script တစ်ခု၏ အမှား သို့မဟုတ် မမျှော်လင့်ထားသော အပြုအမူကို ဖော်ပြသည့် အရာတစ်ခုဖြစ်သည်။
ခြွင်းချက်များကို PHP လုပ်ဆောင်ချက်များနှင့် အတန်းများစွာမှ ချွင်းချက်ထားသည်။
အသုံးပြုသူသတ်မှတ်ထားသော လုပ်ဆောင်ချက်များနှင့် အတန်းများသည် ခြွင်းချက်များကိုလည်း ချွင်းချက်ထားနိုင်သည်။
ခြွင်းချက်များသည် ၎င်းသည် အသုံးမပြုနိုင်သော ဒေတာများတွေ့လာသောအခါ လုပ်ဆောင်ချက်တစ်ခုကို ရပ်တန့်ရန် နည်းလမ်းကောင်းတစ်ခုဖြစ်သည်။
ခြွင်းချက်တစ်ခု လွှင့်ပစ်ခြင်း။
အဆိုပါ throw
ထုတ်ပြန်ချက်သည် အသုံးပြုသူသတ်မှတ်ထားသောလုပ်ဆောင်ချက် သို့မဟုတ် နည်းလမ်းတစ်ခုအား ခြွင်းချက်တစ်ခုအား ထုတ်ပစ်ရန် ခွင့်ပြုသည်။ ခြွင်းချက်တစ်ခုအား ချလိုက်သောအခါ၊ အောက်ပါကုဒ်ကို လုပ်ဆောင်မည်မဟုတ်ပါ။
ခြွင်းချက်တစ်ခုကို မဖမ်းမိပါက၊ "Uncaught Exception" မက်ဆေ့ချ်ဖြင့် ဆိုးရွားသော အမှားတစ်ခု ဖြစ်ပေါ်လိမ့်မည်။
ခြွင်းချက်အနေနဲ့ ဖမ်းမမိအောင် ကြိုးစားကြပါစို့။
ဥပမာ
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>
ရလဒ်သည် ဤကဲ့သို့ ဖြစ်သည်-
Fatal error: Uncaught Exception: Division by zero in
C:\webfolder\test.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4
ကြိုးစား... ဖမ်းတဲ့ ကြေညာချက်
အထက်ဖော်ပြပါ ဥပမာမှ အမှားအယွင်းကို ရှောင်ရှားရန်၊
try...catch
ကျွန်ုပ်တို့သည် ခြွင်းချက်များကို ဖမ်းဆုပ်ပြီး လုပ်ငန်းစဉ်ကို ဆက်လက်လုပ်ဆောင်ရန် ထုတ်ပြန်ချက်ကို အသုံးပြုနိုင်ပါသည်။
အထားအသို
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
ဥပမာ
ခြွင်းချက်တစ်ခု ချလိုက်သောအခါ မက်ဆေ့ချ်ကို ပြပါ-
<?php
function divide($dividend, $divisor) {
if($divisor ==
0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo
divide(5, 0);
} catch(Exception $e) {
echo "Unable to divide.";
}
?>
catch block သည် ခြွင်းချက်အမျိုးအစားကိုဖမ်းမိသင့်ပြီးခြွင်းချက်သို့ဝင်ရောက်ရန်အသုံးပြုနိုင်သည့် variable ၏အမည်ကိုဖော်ပြသည်။ အထက်ဖော်ပြပါ ဥပမာတွင်၊ ခြွင်းချက်အမျိုးအစားသည် Exception
ကိန်းရှင်အမည်ဖြစ်သည် $e
။
ကြိုးစား...ဖမ်း...နောက်ဆုံးထုတ်ပြန်ချက်
ခြွင်းချက်တွေကို ဖမ်းဖို့ ထုတ်ပြန်ချက်ကို try...catch...finally
အသုံးပြုနိုင်ပါတယ်။ finally
ခြွင်းချက်တစ်ခုအား ဖမ်းမိသည်ဖြစ်စေ ဘလောက်ရှိ ကုဒ်သည်
အမြဲတမ်းလည်ပတ်နေမည်ဖြစ်သည်။ ရှိနေပါ
finally
က၊ catch
ပိတ်ဆို့ခြင်းသည် စိတ်ကြိုက်ဖြစ်သည်။
အထားအသို
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
} finally {
code that
always runs regardless of whether an exception was caught
}
ဥပမာ
ခြွင်းချက်တစ်ခုအား ချလိုက်သောအခါ မက်ဆေ့ချ်ကို ပြသပြီး လုပ်ငန်းစဉ် ပြီးဆုံးသွားကြောင်း ဖော်ပြပါ-
<?php
function divide($dividend, $divisor) {
if($divisor
== 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo
divide(5, 0);
} catch(Exception $e) {
echo "Unable to
divide. ";
} finally {
echo "Process complete.";
}
?>
ဥပမာ
ခြွင်းချက်တစ်ခု မဖမ်းမိသော်လည်း စာကြောင်းတစ်ခု ထုတ်သည်-
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo divide(5, 0);
} finally {
echo
"Process complete.";
}
?>
ခြွင်းချက် Object
ခြွင်းချက် Object တွင် လုပ်ဆောင်ချက် ကြုံတွေ့ရသည့် အမှား သို့မဟုတ် မမျှော်လင့်ထားသော အပြုအမူဆိုင်ရာ အချက်အလက် ပါရှိသည်။
အထားအသို
new Exception(message, code, previous)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
message | Optional. A string describing why the exception was thrown |
code | Optional. An integer that can be used used to easily distinguish this exception from others of the same type |
previous | Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter |
နည်းလမ်းများ
ခြွင်းချက်တစ်ခုအား ဖမ်းမိသောအခါ၊ ခြွင်းချက်နှင့်ပတ်သက်သော အချက်အလက်များကို ရယူရန် အောက်ပါဇယားတွင် အသုံးပြုနိုင်သည့် နည်းလမ်းအချို့ကို ပြသသည်-
Method | Description |
---|---|
getMessage() | Returns a string describing why the exception was thrown |
getPrevious() | If this exception was triggered by another one, this method returns the previous exception. If not, then it returns null |
getCode() | Returns the exception code |
getFile() | Returns the full path of the file in which the exception was thrown |
getLine() | Returns the line number of the line of code which threw the exception |
ဥပမာ
ချွင်းချက်တစ်ခုအကြောင်း အချက်အလက်များ ထုတ်ပေးသည်-
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero", 1);
}
return $dividend / $divisor;
}
try {
echo
divide(5, 0);
} catch(Exception $ex) {
$code = $ex->getCode();
$message = $ex->getMessage();
$file = $ex->getFile();
$line = $ex->getLine();
echo "Exception thrown in $file on line
$line: [Code $code]
$message";
}
?>
ခြွင်းချက် အကိုးအကား အပြည့်အစုံ
အပြည့်အစုံကိုးကားရန်အတွက်၊ ကျွန်ုပ်တို့၏ ပြီးပြည့်စုံသော PHP Exception Reference သို့ သွားပါ ။
ကိုးကားချက်တွင် ခြွင်းချက်နည်းလမ်းအားလုံး၏ ဖော်ပြချက်များနှင့် ဥပမာများ ပါရှိသည်။