PHP İstisnaları
İstisna nedir?
İstisna, bir PHP betiğinin bir hatasını veya beklenmeyen davranışını tanımlayan bir nesnedir.
İstisnalar birçok PHP işlevi ve sınıfı tarafından oluşturulur.
Kullanıcı tanımlı işlevler ve sınıflar da istisnalar atabilir.
İstisnalar, bir işlevi kullanamayacağı verilerle karşılaştığında durdurmanın iyi bir yoludur.
İstisna Atma
İfade throw
, kullanıcı tanımlı bir işlev veya yöntemin bir istisna atmasına izin verir. Bir istisna atıldığında, onu takip eden kod yürütülmez.
Bir istisna yakalanmazsa, "Yakalanmayan İstisna" mesajıyla önemli bir hata oluşur.
Onu yakalamadan bir istisna atmaya çalışalım:
Örnek
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>
Sonuç şöyle görünecek:
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 Bildirimi
Yukarıdaki örnekteki hatayı önlemek için,
try...catch
istisnaları yakalamak ve işleme devam etmek için ifadeyi kullanabiliriz.
Sözdizimi
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
Örnek
Bir istisna atıldığında bir mesaj göster:
<?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 bloğu, ne tür bir istisnanın yakalanması gerektiğini ve istisnaya erişmek için kullanılabilecek değişkenin adını gösterir. Yukarıdaki örnekte, istisna türü Exception
ve değişken adı ise $e
.
try...catch...nihai Açıklama
İfade try...catch...finally
, istisnaları yakalamak için kullanılabilir. Bloktaki kod,
finally
bir istisna yakalanıp yakalanmadığına bakılmaksızın her zaman çalışır. Varsa
finally
, catch
blok isteğe bağlıdır.
Sözdizimi
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
}
Örnek
Bir istisna atıldığında bir mesaj gösterin ve ardından işlemin sona erdiğini belirtin:
<?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.";
}
?>
Örnek
Bir istisna yakalanmamış olsa bile bir dize çıktısı alın:
<?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.";
}
?>
İstisna Nesnesi
İstisna Nesnesi, işlevin karşılaştığı hata veya beklenmeyen davranış hakkında bilgi içerir.
Sözdizimi
new Exception(message, code, previous)
Parametre Değerleri
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 |
yöntemler
Bir istisna yakalarken, aşağıdaki tablo istisna hakkında bilgi almak için kullanılabilecek yöntemlerden bazılarını gösterir:
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 |
Örnek
Atılan bir istisna hakkında çıktı bilgileri:
<?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";
}
?>
Eksiksiz İstisna Referansı
Tam bir referans için, Tam PHP İstisna Referansımıza gidin .
Referans, tüm İstisna yöntemlerinin açıklamalarını ve örneklerini içerir.