Pengecualian PHP
Apa itu Pengecualian?
Pengecualian adalah objek yang menjelaskan kesalahan atau perilaku tak terduga dari skrip PHP.
Pengecualian dilemparkan oleh banyak fungsi dan kelas PHP.
Fungsi dan kelas yang ditentukan pengguna juga dapat membuang pengecualian.
Pengecualian adalah cara yang baik untuk menghentikan suatu fungsi ketika menemukan data yang tidak dapat digunakan.
Melempar Pengecualian
Pernyataan throw
tersebut memungkinkan fungsi atau metode yang ditentukan pengguna untuk melempar pengecualian. Ketika pengecualian dilemparkan, kode yang mengikutinya tidak akan dieksekusi.
Jika pengecualian tidak tertangkap, kesalahan fatal akan terjadi dengan pesan "Pengecualian Tidak Tertangkap".
Mari kita coba melempar pengecualian tanpa menangkapnya:
Contoh
<?php
function divide($dividend, $divisor) {
if($divisor == 0)
{
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
?>
Hasilnya akan terlihat seperti ini:
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
Pernyataan try...catch
Untuk menghindari kesalahan dari contoh di atas, kita dapat menggunakan
try...catch
pernyataan untuk menangkap pengecualian dan melanjutkan proses.
Sintaksis
try {
code that can throw exceptions
} catch(Exception $e) {
code that runs when an exception is caught
}
Contoh
Tampilkan pesan saat pengecualian dilemparkan:
<?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.";
}
?>
Blok catch menunjukkan jenis pengecualian apa yang harus ditangkap dan nama variabel yang dapat digunakan untuk mengakses pengecualian. Dalam contoh di atas, jenis pengecualiannya adalah Exception
dan nama variabelnya adalah $e
.
Mencoba...menangkap...akhirnya Pernyataan
Pernyataan try...catch...finally
dapat digunakan untuk menangkap pengecualian. Kode di
finally
blok akan selalu berjalan terlepas dari apakah pengecualian tertangkap. Jika
finally
ada, catch
blok adalah opsional.
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
}
Contoh
Tampilkan pesan ketika pengecualian dilemparkan dan kemudian tunjukkan bahwa proses telah berakhir:
<?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.";
}
?>
Contoh
Keluarkan string bahkan jika pengecualian tidak ditangkap:
<?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.";
}
?>
Objek Pengecualian
Objek Pengecualian berisi informasi tentang kesalahan atau perilaku tak terduga yang ditemui fungsi tersebut.
Sintaksis
new Exception(message, code, previous)
Nilai Parameter
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 |
Metode
Saat menangkap pengecualian, tabel berikut menunjukkan beberapa metode yang dapat digunakan untuk mendapatkan informasi tentang pengecualian:
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 |
Contoh
Keluarkan informasi tentang pengecualian yang dilemparkan:
<?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";
}
?>
Referensi Pengecualian Lengkap
Untuk referensi lengkap, buka Referensi Pengecualian PHP Lengkap kami .
Referensi berisi deskripsi dan contoh semua metode Pengecualian.