Rumah > pembangunan bahagian belakang > tutorial php > PHP异常测试,示例二原创

PHP异常测试,示例二原创

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-23 13:38:05
asal
997 orang telah melayarinya

<?php /* * 测试代码 * 主要测试异常的示例 *//*//创建可抛出一个异常的函数,示例一function checkNum($number) {    if ($number > 1) {        throw new Exception("Value must be 1 or below");    }    return true;}//在 "try" 代码块中触发异常try {    checkNum(2);    //If the exception is thrown, this text will not be shown    echo 'If you see this, the number is 1 or below';}//捕获异常catch (Exception $e) {    echo 'Message: ' . $e->getMessage();} *  *///---------------------//创建可抛出一个异常的函数,示例二function checkNum($number) {    try{        if ($number > 1) {            throw new Exception("Value must be 1 or below");        }    }catch (Exception $e) {        echo 'Message: ' . $e->getMessage();    }    return true;}try{    checkNum(2);    echo ' If you see this, the number is 1 or below';}catch (Exception $e) {    echo 'Error 2 : ' . $e->getMessage();}echo "<br>It is work good.";
Salin selepas log masuk

示例一,是网上已有的

示例二,是测试自己的想法的。


Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan