欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php異常處理方法實例匯總

 更新時間:2015年06月24日 10:42:43   作者:zhuzhao  
這篇文章主要介紹了php異常處理方法,實例匯總了常見的php異常處理技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了php異常處理方法。分享給大家供大家參考。具體如下:

<?php 
$path = "D://in.txt"; 
try //檢測異常 
{ 
  file_open($path); 
} 
catch(Exception $e) //捕獲異常 
{ 
  echo $e->getMessage(); 
} 
 
function file_open($path) 
{ 
  if(!file_exists($path))
  //如果文件無法找到,拋出異常對象 
  { 
    throw new Exception("文件無法找到", 1); 
  } 
  if(!fopen($path, "r"))
  //如果文件無法打開,拋出異常對象 
  { 
    throw new Exception("文件無法打開", 2); 
  } 
} 
?> 

<?php 
$path = "D://in.txt";
//文件所在路徑 
file_open($path);
//調(diào)用file_open函數(shù) 
 
function file_open($path) 
{ 
  if(!file_exists($path))
  //如果文件無法找到,拋出異常對象 
  { 
    throw new Exception("文件無法找到", 1); 
  } 
  if(!fopen($path, "r"))
  //如果文件無法打開,拋出異常對象 
  { 
    throw new Exception("文件無法打開", 2); 
  } 
} 
?> 

<?php 
function exception_handler($e)
//用于處理異常的函數(shù) 
{ 
 echo "未捕獲的異常:".$e->getMessage(); 
} 
 
set_exception_handler("exception_handler");
//設置異常處理函數(shù) 
try //檢測異常 
{ 
  $path = "D://in.txt"; 
} 
catch(Exception $e) //捕獲異常 
{ 
  echo $e->getMessage(); 
} 
file_open($path); //調(diào)用函數(shù)打開文件 
function file_open($path) 
{ 
  if(!file_exists($path))
  //如果文件無法找到,拋出異常對象 
  { 
    throw new Exception("文件無法找到", 1); 
  } 
  if(!fopen($path, "r"))
  //如果文件無法打開,拋出異常對象 
  { 
    throw new Exception("文件無法打開", 2); 
  } 
} 
?> 
<?php 
$path = "D://in.txt"; 
 
try 
{ 
  file_open($path); //嘗試打開文件 
} 
catch(Exception $e) 
{ 
  echo "異常信息:".$e->getMessage()."/n";
  //返回用戶自定義的異常信息 
  echo "異常代碼:".$e->getCode()."/n";
  //返回用戶自定義的異常代碼 
  echo "文件名:".$e->getFile()."/n";
  //返回發(fā)生異常的PHP程序文件名 
  echo "異常代碼所在行".$e->getLine()."/n";
  //返回發(fā)生異常的代碼所在行的行號 
  echo "傳遞路線:"; 
  print_r($e->getTrace());
  //以數(shù)組形式返回跟蹤異常每一步傳遞的路線 
  echo $e->getTraceAsString();
  //返回格式化成字符串的getTrace函數(shù)信息 
} 
function file_open($path) 
{ 
  if(!file_exists($path))
  //如果文件不存在,則拋出錯誤 
  { 
    throw new Exception("文件無法找到", 1); 
  } 
   
  if(!fopen($path, "r")) 
  { 
    throw new Exception("文件無法打開", 2); 
  } 
} 
?> 

<?php 
class FileExistsException extends Exception{}
//用于處理文件不存在異常的類 
class FileOpenException extends Exception{}
//用于處理文件不可讀異常的類 
$path = "D://in.txt"; 
try 
{ 
  file_open($path); 
} 
catch(FileExistsException $e)
//如果產(chǎn)生FileExistsException異常則提示用戶確認文件位置 
{ 
  echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n"; 
  echo "請確認文件位置。"; 
} 
catch(FileOpenException $e)
//如果產(chǎn)生FileOpenException異常則提示用戶確認文件的可讀性 
{ 
  echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n"; 
  echo "請確認文件的可讀性。"; 
} 
catch(Exception $e) 
{ 
  echo "[未知異常]"; 
  echo "異常信息:".$e->getMessage()."/n";
  //返回用戶自定義的異常信息 
  echo "異常代碼:".$e->getCode()."/n";
  //返回用戶自定義的異常代碼 
  echo "文件名:".$e->getFile()."/n";
  //返回發(fā)生異常的PHP程序文件名 
  echo "異常代碼所在行".$e->getLine()."/n";
  //返回發(fā)生異常的代碼所在行的行號 
  echo "傳遞路線:"; 
  print_r($e->getTrace());
  //以數(shù)組形式返回跟蹤異常每一步傳遞的路線 
  echo $e->getTraceAsString();
  //返回格式化成字符串的getTrace函數(shù)信息 
} 
function file_open($path) 
{ 
  if(!file_exists($path)) 
  { 
    throw new FileExistsException("文件無法找到", 1);
    //拋出FileExistsException異常對象 
  } 
   
  if(!fopen($path, "r")) 
  { 
    throw new FileOpenException("文件無法打開", 2);
    //拋出FileOpenException異常對象 
 
  } 
} 
?> 

<?php 
class FileExistsException extends Exception{}
//用于處理文件不存在異常的類 
class FileOpenException extends Exception{}
//用于處理文件不可讀異常的類 
$path = "D://in.txt"; 
try 
{ 
  file_open($path); //嘗試打開文件 
} 
catch(Exception $e) 
{ 
  echo "[未知異常]"; 
  echo "異常信息:".$e->getMessage()."/n";
  //返回用戶自定義的異常信息 
  echo "異常代碼:".$e->getCode()."/n";
  //返回用戶自定義的異常代碼 
  echo "文件名:".$e->getFile()."/n";
  //返回發(fā)生異常的PHP程序文件名 
  echo "異常代碼所在行".$e->getLine()."/n";
  //返回發(fā)生異常的代碼所在行的行號 
  echo "傳遞路線:"; 
  print_r($e->getTrace());
  //以數(shù)組形式返回跟蹤異常每一步傳遞的路線 
  echo $e->getTraceAsString();
  //返回格式化成字符串的getTrace函數(shù)信息 
} 
catch(FileExistsException $e)
//如果產(chǎn)生FileExistsException異常則提示用戶確認文件位置 
{ 
  echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n"; 
  echo "請確認文件位置。"; 
} 
catch(FileOpenException $e)
//如果產(chǎn)生FileOpenException異常則提示用戶確認文件的可讀性 
{ 
  echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n"; 
  echo "請確認文件的可讀性。"; 
} 
function file_open($path) 
{ 
  if(!file_exists($path))
  //如果文件不存在,則輸出錯誤 
  { 
    throw new FileExistsException("文件無法找到", 1); 
  } 
   
  if(!fopen($path, "r")) 
  { 
    throw new FileOpenException("文件無法打開", 2); 
  } 
} 
?> 

<?php 
class FileExistsException extends Exception{}
//用于處理文件不存在異常的類 
class FileOpenException extends Exception{}
//用于處理文件不可讀異常的類 
$path = "D://in.txt"; 
try 
{ 
  file_open($path); 
} 
catch(FileExistsException $e)
//如果產(chǎn)生FileExistsException異常則提示用戶確認文件位置 
{ 
  echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n"; 
  echo "請確認文件位置。"; 
} 
catch(FileOpenException $e)
//如果產(chǎn)生FileOpenException異常則提示用戶確認文件的可讀性 
{ 
  echo "程序在運行過程中發(fā)生了異常:".$e->getMessage()."/n"; 
  echo "請確認文件的可讀性。"; 
} 
catch(Exception $e) 
{ 
  echo "[未知異常]"; 
  echo "異常信息:".$e->getMessage()."/n";
  //返回用戶自定義的異常信息 
  echo "異常代碼:".$e->getCode()."/n";
  //返回用戶自定義的異常代碼 
  echo "文件名:".$e->getFile()."/n";
  //返回發(fā)生異常的PHP程序文件名 
  echo "異常代碼所在行".$e->getLine()."/n";
  //返回發(fā)生異常的代碼所在行的行號 
  echo "傳遞路線:"; 
  print_r($e->getTrace());
  //以數(shù)組形式返回跟蹤異常每一步傳遞的路線 
  echo $e->getTraceAsString();
  //返回格式化成字符串的getTrace函數(shù)信息 
} 
function file_open($path) 
{ 
  try 
  { 
    if(!file_exists($path)) 
    { 
      throw new FileExistsException("文件無法找到", 1); 
    } 
     
    if(!fopen($path, "r")) 
    { 
      throw new FileOpenException("文件無法打開", 2); 
    } 
  } 
  catch(Exception $e)               //捕獲異常 
  { 
    echo "file_open函數(shù)在運行過程中出現(xiàn)異常"; 
    throw $e;                  //重擲異常 
  } 
} 
?>

希望本文所述對大家的php程序設計有所幫助。

相關(guān)文章

  • PHP中str_replace函數(shù)使用小結(jié)

    PHP中str_replace函數(shù)使用小結(jié)

    在實際的程序開發(fā)中,執(zhí)行字符串替換操作是一件非常經(jīng)常的事,對str_replace函數(shù)的實用也會非常頻繁。
    2008-10-10
  • PHP獲取搜索引擎關(guān)鍵字來源的函數(shù)(支持百度和谷歌等搜索引擎)

    PHP獲取搜索引擎關(guān)鍵字來源的函數(shù)(支持百度和谷歌等搜索引擎)

    通過網(wǎng)站功能來增加一字段,獲取來源關(guān)鍵字,如何獲取來源關(guān)鍵字,代碼發(fā)在下面,里面包含(百度、谷歌、雅虎、搜狗、搜搜、必應、有道)幾大搜索引擎的獲取方法,代碼中均注明,希望對你有幫助,分享一下
    2012-10-10
  • 使用php統(tǒng)計字符串中中英文字符的個數(shù)

    使用php統(tǒng)計字符串中中英文字符的個數(shù)

    本篇文章是對使用php統(tǒng)計字符串中中英文字符的個數(shù)的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • php性能優(yōu)化分析工具XDebug 大型網(wǎng)站調(diào)試工具

    php性能優(yōu)化分析工具XDebug 大型網(wǎng)站調(diào)試工具

    大型網(wǎng)站調(diào)試工具之一(php性能優(yōu)化分析工具XDebug) ,開發(fā)php的朋友可以參考下。有助于解決php代碼的多種問題。
    2011-05-05
  • 比較strtr, str_replace和preg_replace三個函數(shù)的效率

    比較strtr, str_replace和preg_replace三個函數(shù)的效率

    本篇文章是對strtr, str_replace和preg_replace三個函數(shù)的效率問題進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP詳細徹底學習Smarty

    PHP詳細徹底學習Smarty

    Smarty是一個php模板引擎,它分開了邏輯程序和外在的內(nèi)容,提供了一種易于管理的方法. Smarty要求web服務器運行php4.0.6和以上版本. smarty安裝需要smarty庫文件??梢匀ス俜骄W(wǎng)站http://smarty.php.net下載。 網(wǎng)上講了很多安裝的教程,但是我都沒有成功,所以直接把整個目錄名改為smarty直接復制到了網(wǎng)站所在的目錄下,然后打開http://網(wǎng)站路徑/smarty/demo/index.php,顯示正常,應該算是安裝成功了。
    2008-03-03
  • 關(guān)于php 高并發(fā)解決的一點思路

    關(guān)于php 高并發(fā)解決的一點思路

    涉及搶購、秒殺、抽獎、搶票等活動時,為了避免超賣,那么庫存數(shù)量是有限的,但是如果同時下單人數(shù)超過了庫存數(shù)量,就會導致商品超賣問題。那么我們怎么來解決這個問題呢,我的思路如下
    2017-04-04
  • php+html5+ajax實現(xiàn)上傳圖片的方法

    php+html5+ajax實現(xiàn)上傳圖片的方法

    這篇文章主要介紹了php+html5+ajax實現(xiàn)上傳圖片的方法,對比分析了js原生及jQuery兩種ajax調(diào)用上傳圖片的方法,以及php圖片上傳處理等技巧,需要的朋友可以參考下
    2016-05-05
  • PHP單例模式簡單用法示例

    PHP單例模式簡單用法示例

    這篇文章主要介紹了PHP單例模式簡單用法,結(jié)合數(shù)據(jù)庫操作類實例形式分析了php單例模式的具體實現(xiàn)方法與使用技巧,需要的朋友可以參考下
    2017-06-06
  • php mysql操作mysql_connect連接數(shù)據(jù)庫實例詳解

    php mysql操作mysql_connect連接數(shù)據(jù)庫實例詳解

    php操作數(shù)據(jù)庫首先必須連接到指定的數(shù)據(jù)庫,連接數(shù)據(jù)庫可以使用PHP mysql_connect函數(shù),本文章向大家介紹mysql_connect函數(shù)的使用方法和實例,需要的朋友可以參考一下
    2016-12-12

最新評論