throw ifadesi: Eğer bir fonksiyonun exception oluşturma olasılığı varsa ve try/catch bloğu kullanılmak istenmiyorsa özel hata oluşturmak için kullanılır.
finally ifadesi: try catch bloklarından sonra ne olursa olsun kodu çalıştırmak için kullanılır.
try ifadesi: Hata oluşturabilecek kod blokları yerleştirilir.
catch ifadesi: hata oluştuğunda hatayı yakalamak için kullanılır.
if ($item == null) {
throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
}
if ($item === null) { // item does not exist
throw new \yii\web\HttpException(404, 'The requested Item could not be found.');
}
Error hata sayfasına yönlendirmek için frontend/web/index.php
dosyasında
//defined('YII_DEBUG') or define('YII_DEBUG', true);
yukarıda ki gibi kodları yorum satırına almanız gerekecektir. Ayrıca bir sorgunun sonucu boş geldiği zaman yine aşağıda ki gibi, hata sayfasına yönlendirebilirsiniz. Buna hata sayfasını çağırmakta denir.
$customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
if ($customer === null){
throw new \yii\web\NotFoundHttpException("Your Error Message.");
}