PHP Exception capture

Source: Internet
Author: User
Tags getmessage

1 First is try,Catch<?PHP$path= "D:\\in.txt";Try           //Detecting Anomalies{File_open ($path);}Catch(Exception $e)//Catching exceptions{ Echo $e-getMessage ();}functionFile_open ($path){ if(!file_exists($path))//throws an exception object if the file cannot be found {  Throw New Exception("File cannot be found", 1); }  if(!fopen($path, "R"))//throws an exception object if the file cannot be opened {  Throw New Exception("File cannot be opened", 2); }}?>Note with$e->getmessage () output exception information. 2Output exception Complete information<?PHP$path= "D:\\in.txt";Try{File_open ($path);//try to open a file}Catch(Exception $e){ Echo"Exception information:".$e->getmessage (). " \ n ";//returns user-defined exception information Echo"Exception code:".$e->getcode (). " \ n ";//returns the user-defined exception code Echo"File name:".$e->getfile (). " \ n ";//returns the PHP program file name where the exception occurred EchoThe line where the exception code is located.$e->getline (). " \ n ";//returns the line number of the line in which the exception occurred Echo"Delivery route:"; Print_r($e->gettrace ());//returns an array of trace exceptions for each step of the route passed Echo $e->gettraceasstring ();//returns the Gettrace function information formatted as a string}functionFile_open ($path){ if(!file_exists($path))//throws an error if the file does not exist {  Throw New Exception("File cannot be found", 1); }  if(!fopen($path, "R")) {  Throw New Exception("File cannot be opened", 2); }}? >3 Extension Exception,that is, a custom exception<?PHPclassFileexistsexceptionextends Exception{}//class for handling files that do not exist exceptionsclassFileopenexceptionextends Exception{}//class for handling file non-readable exceptions$path= "D:\\in.txt";Try{File_open ($path);}Catch(fileexistsexception$e)//Prompt user to confirm file location If Fileexistsexception exception is generated{ Echo"An exception occurred during the run of the program:".$e->getmessage (). " \ n "; Echo"Please confirm the file location. ";}Catch(fileopenexception$e)//prompts the user to confirm the readability of the file if a Fileopenexception exception is generated{ Echo"An exception occurred during the run of the program:".$e->getmessage (). " \ n "; Echo"Please confirm the readability of the file. ";}Catch(Exception $e){ Echo"[Unknown Exception]"; Echo"Exception information:".$e->getmessage (). " \ n ";//returns user-defined exception information Echo"Exception code:".$e->getcode (). " \ n ";//returns the user-defined exception code Echo"File name:".$e->getfile (). " \ n ";//returns the PHP program file name where the exception occurred EchoThe line where the exception code is located.$e->getline (). " \ n ";//returns the line number of the line in which the exception occurred Echo"Delivery route:"; Print_r($e->gettrace ());//returns an array of trace exceptions for each step of the route passed Echo $e->gettraceasstring ();//returns the Gettrace function information formatted as a string}functionFile_open ($path){ if(!file_exists($path)) {  Throw NewFileexistsexception ("File cannot be found", 1);//Throw Fileexistsexception Exception Object }  if(!fopen($path, "R")) {  Throw NewFileopenexception ("File cannot be opened", 2);//Throw Fileopenexception Exception Object }}? >4re-throwing anomalies to the upper<?PHPclassFileexistsexceptionextends Exception{}//class for handling files that do not exist exceptionsclassFileopenexceptionextends Exception{}//class for handling file non-readable exceptions$path= "D:\\in.txt";Try{File_open ($path);}Catch(fileexistsexception$e)//Prompt user to confirm file location If Fileexistsexception exception is generated{ Echo"An exception occurred during the run of the program:".$e->getmessage (). " \ n "; Echo"Please confirm the file location. ";}Catch(fileopenexception$e)//prompts the user to confirm the readability of the file if a Fileopenexception exception is generated{ Echo"An exception occurred during the run of the program:".$e->getmessage (). " \ n "; Echo"Please confirm the readability of the file. ";}Catch(Exception $e){ Echo"[Unknown Exception]"; Echo"Exception information:".$e->getmessage (). " \ n ";//returns user-defined exception information Echo"Exception code:".$e->getcode (). " \ n ";//returns the user-defined exception code Echo"File name:".$e->getfile (). " \ n ";//returns the PHP program file name where the exception occurred EchoThe line where the exception code is located.$e->getline (). " \ n ";//returns the line number of the line in which the exception occurred Echo"Delivery route:"; Print_r($e->gettrace ());//returns an array of trace exceptions for each step of the route passed Echo $e->gettraceasstring ();//returns the Gettrace function information formatted as a string}functionFile_open ($path){ Try {  if(!file_exists($path))  {   Throw NewFileexistsexception ("File not Found", 1); }    if(!fopen($path, "R"))  {   Throw NewFileopenexception ("File cannot be opened", 2); } } Catch(Exception $e)//Catching exceptions {  Echo"An exception occurred during the operation of the File_open function"; Throw $e;//Throw Exception }}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.