<?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怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号