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 }}?>