1. 程式人生 > 實用技巧 >php丟擲異常

php丟擲異常

<?php
//建立可丟擲一個異常的函式
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}

checkNum(2);
?>


Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' in G:\phpStudy\Public\zititest.php:7 Stack trace: #0 G:\phpStudy\WWW\mircoweb\mircoweb\wwwroot\Public\zititest.php(12): checkNum(2) #1 {main} thrown inG:\phpStudy\WWW\mircoweb\mircoweb\wwwroot\Public\zititest.php

on line7

<?php
//建立可丟擲一個異常的函式
function checkNum($number)
 {
 if($number>1)
  {
  throw new Exception("Value must be 111 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(); } ?>

Message: Value must be 111 or below