PHP7捕获错误异常方法

这种 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 IlluminateSupportFacadesSchema; use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; /** * 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.

原文地址:https://www.cnblogs.com/2881064178dinfeng/p/6145803.html