PHP -uitsonderings
Wat is 'n uitsondering?
'n Uitsondering is 'n voorwerp wat 'n fout of onverwagte gedrag van 'n PHP-skrip beskryf.
Uitsonderings word gegooi deur baie PHP-funksies en -klasse.
Gebruiker gedefinieerde funksies en klasse kan ook uitsonderings gooi.
Uitsonderings is 'n goeie manier om 'n funksie te stop wanneer dit op data afkom wat dit nie kan gebruik nie.
Gooi 'n uitsondering
Die throw
stelling laat 'n gebruikergedefinieerde funksie of metode toe om 'n uitsondering te gooi. Wanneer 'n uitsondering gegooi word, sal die kode wat daarop volg nie uitgevoer word nie.
As 'n uitsondering nie gevang word nie, sal 'n noodlottige fout voorkom met 'n "Ongevang Uitsondering" boodskap.
Kom ons probeer om 'n uitsondering te gooi sonder om dit te vang:
Voorbeeld
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>
Die resultaat sal iets soos volg lyk:
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
Die probeer...vang Verklaring
Om die fout van die voorbeeld hierbo te vermy, kan ons die
try...catch
stelling gebruik om uitsonderings op te vang en die proses voort te sit.
Sintaksis
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
Voorbeeld
Wys 'n boodskap wanneer 'n uitsondering gegooi word:
<?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.";
}
?>
Die vangblok dui aan watter tipe uitsondering gevang moet word en die naam van die veranderlike wat gebruik kan word om toegang tot die uitsondering te verkry. In die voorbeeld hierbo is die tipe uitsondering Exception
en die veranderlike naam is $e
.
Die probeer...vang...uiteindelik Verklaring
Die try...catch...finally
stelling kan gebruik word om uitsonderings te vang. Kode in die
finally
blok sal altyd loop, ongeag of 'n uitsondering gevang is. Indien
finally
teenwoordig is, is die catch
blok opsioneel.
Sintaksis
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
}
Voorbeeld
Wys 'n boodskap wanneer 'n uitsondering gegooi word en dui dan aan dat die proses beëindig is:
<?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.";
}
?>
Voorbeeld
Voer 'n string uit, selfs al is 'n uitsondering nie opgespoor nie:
<?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.";
}
?>
Die Uitsonderingsvoorwerp
Die Uitsonderingsvoorwerp bevat inligting oor die fout of onverwagte gedrag wat die funksie teëgekom het.
Sintaksis
new Exception(message, code, previous)
Parameterwaardes
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 |
Metodes
Wanneer 'n uitsondering gevang word, toon die volgende tabel sommige van die metodes wat gebruik kan word om inligting oor die uitsondering te kry:
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 |
Voorbeeld
Voer inligting uit oor 'n uitsondering wat gegooi is:
<?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";
}
?>
Voltooi uitsonderingsverwysing
Vir 'n volledige verwysing, gaan na ons Volledige PHP-uitsonderingsverwysing .
Die verwysing bevat beskrywings en voorbeelde van alle uitsonderingsmetodes.