欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP异常测试,示例二原创

程序员文章站 2022-05-17 08:20:49
...
 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 "
It is work good.";

示例一,是网上已有的

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