首页 > Web开发 > 详细

PHP异常测试,示例二原创

时间:2015-03-23 21:47:36      阅读:373      评论:0      收藏:0      [点我收藏+]
<?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.";

示例一,是网上已有的

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


PHP异常测试,示例二原创

原文:http://blog.csdn.net/wide288/article/details/44570021

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!