This article mainly introduces the common PHP exception handling methods, interested in the friend's reference, I hope to help you.
This article describes the PHP exception handling methods, as follows:
<?php $path = "D://in.txt"; Try//Detect exception { File_open ($path),} catch (Exception $e)//catch Exception { echo $e->getmessage ();} function File_open ($path) { if (!file_exists ($path)) //If the file cannot be found, throw the exception object { throw new Exception (" File could not be found ", 1); } if (!fopen ($path, "R")) //If the file cannot be opened, throw the exception object { throw new Exception ("File cannot be opened", 2);} }?>
<?php $path = "d://in.txt";//the path where the file is located file_open ($path);//Call File_open function File_open ($path) { if (! File_exists ($path)) //If the file cannot be found, throw the exception object { throw new Exception ("File cannot be found", 1); } if (!fopen ($path, "R")) //If the file cannot be opened, throw the exception object { throw new Exception ("File cannot be opened", 2);} }?>
<?php function Exception_handler ($e)//functions for handling Exceptions {echo "uncaught exception:". $e GetMessage (); } set_exception_handler ("Exception_handler");//Set exception handling function try//detect exception {$path = "d://in.txt";} catch (Exception $e)/catch exception {echo $e->getmessage ();} file_open ($path); The calling function opens the file functions File_open ($path) {if (!file_exists ($path))///If the file cannot be found, throws the exception object {throw new Exception ("File cannot be found", 1); if (!fopen ($path, "R"))//If the file cannot be opened, throw the exception object {throw new Exception ("File cannot be opened", 2); }};
<?php $path = "D://in.txt"; try {file_open ($path);//attempt to open the file} catch (Exception $e) {echo "Exception information:". $e->getmessage (). " /n "; Returns the 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 echo "line of exception code." $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 echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string} function File_open ($path) {if (!file_exists ($path))//If the file does not exist, throws an error {throw new Except Ion ("File cannot be found", 1); } if (!fopen ($path, "R")) {throw new Exception ("File cannot be opened", 2); }};
<?php class Fileexistsexception extends exception{}//for handling files with no exception classes Fileopenexception extends exception{}// Class $path = "D://in.txt" for handling non-readable file exceptions; try {file_open ($path);} catch (Fileexistsexception $e)//If a fileexistsexception exception is generated, the user is prompted to confirm that the file location {echo "program has an exception during the run:". $e->getmessage (). " /n "; echo "Please confirm the file location. "; } catch (Fileopenexception $e)//If a fileopenexception exception is generated, the user is prompted to confirm the readability of the file {echo] The exception occurred during the run: ". $e->getmessage ()." /n "; echo "Please confirm the readability of the file. "; } catch (Exception $e) {echo "[unknown exception]"; echo "Exception information:". $e->getmessage (). " /n "; Returns the 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 echo "line of exception code." $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 echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string} function File_open ($path) {if (!file_exists ($path)) {throw new Fileexistsexception ("File Could not be found ", 1); Throw Fileexistsexception Exception Object} if (!fopen ($path, "R")) {throw new Fileopenexception ("File cannot be opened", 2); Throw fileopenexception exception Object}}?>
<?php class Fileexistsexception extends exception{}//for handling files with no exception classes Fileopenexception extends exception{}// Class $path = "D://in.txt" for handling non-readable file exceptions; try {file_open ($path);//attempt to open file} catch (Exception $e) {echo "[unknown exception]"; echo "Exception information:". $e->getmessage (). " /n "; Returns the 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 echo "line of exception code." $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 echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string} catch (Fileexistsexception $e)//Prompts the user to confirm the file location {echo If a Fileexistsexception exception is generated) An exception occurred during the run of the program: ". $e->getmessage ()." /n "; echo "Please confirm the file location. "; } catch (Fileopenexception $e)//If a fileopenexception exception is generated, the user is prompted to confirm the readability of the file {echo] The exception occurred during the run: ". $e->getmessage ()." /n "; echo "Please confirm the readability of the file. "; } function File_open ($path) {if (!file_exists ($path))//If the file does not exist, output error {throw new Fileexistsexception ("File cannot be found", 1); } if(!fopen ($path, "R")) {throw new Fileopenexception ("File cannot be opened", 2); }}?>
<?php class Fileexistsexception extends exception{}//for handling files with no exception classes Fileopenexception extends exception{}// Class $path = "D://in.txt" for handling non-readable file exceptions; try {file_open ($path);} catch (Fileexistsexception $e)//If a fileexistsexception exception is generated, the user is prompted to confirm that the file location {echo "program has an exception during the run:". $e->getmessage (). " /n "; echo "Please confirm the file location. "; } catch (Fileopenexception $e)//If a fileopenexception exception is generated, the user is prompted to confirm the readability of the file {echo] The exception occurred during the run: ". $e->getmessage ()." /n "; echo "Please confirm the readability of the file. "; } catch (Exception $e) {echo "[unknown exception]"; echo "Exception information:". $e->getmessage (). " /n "; Returns the 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 echo "line of exception code." $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 echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string} function File_open ($path) {try {if (!file_exists ($path)) {throw new Fileexi Stsexception ("File cannot be found", 1); } if (!foPen ($path, "R")) {throw new Fileopenexception ("File cannot be opened", 2); }} catch (Exception $e)//catch Exception {echo ' File_open function is abnormal during operation "; Throw $e; Re-Throw Exception}}?>
Summary: the above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Implementation techniques of PHP unordered tree
PHP recursive traversal for unlimited classification
PHP Process Control and mathematical operations