這種 Error 異常可以像 Exception 異常一樣被第一個匹配的 try / catch 塊所捕獲。如果沒有匹配的 catch 塊,則調用異常處理函數(事先通過 set_exception_handler() 注冊)進行處理。 如果尚未注冊異常處理函數,則按照傳統方式處理:被報告為一個致命錯誤(Fatal Error)。
Error 類並非繼承自 Exception 類,所以不能用 catch (Exception $e) { … } 來捕獲 Error。你可以用 catch (Error $e) { … },或者通過注冊異常處理函數( set_exception_handler())來捕獲 Error。
來一段代碼實例
try {
echo asdfasdf('1'); //未定義的函數
} catch (Exception $e) {
// Handle exception
echo 'Exception';
} catch (Error $e) { // Clearly a different type of object
// Log error and end gracefully
echo 'Error';
}
最后輸出的是 Error 。。。
所以用PHP7捕獲異常防止錯誤的話,建議 catch :Exception 和 Error
<php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; /** * Migration auto-generated by Sequel Pro Laravel Export * @see https://github.com/cviebrock/sequel-pro-laravel-export */ class CreateCategoriesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); $table->integer('wp_id'); $table->string('name', 255); $table->string('slug', 255); $table->text('description'); $table->nullableTimestamps(); $table->unique('slug', 'categories_slug_unique'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('categories'); } }
The migration file will then be saved to your desktop, and you can move it to your projects migrations directory, so it’s ready to be used through the Artisan command.