1 First of all, Try,catch.
$path = "D:\\\\in.txt";
Try//Detect exceptions
{
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 an exception object
{
throw new Exception ("File cannot be found", 1);
}
if (!fopen ($path, "R"))//If the file cannot be opened, throw an exception object
{
throw new Exception ("File cannot be opened", 2);
}
}
?>
Note Use $e->getmessage () to output exception information.
2 Output Exception complete information
$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 file name of the PHP program 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 passed
echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string
}
function File_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);
}
}
?>
Extended exception, or custom exception
Class Fileexistsexception extends exception{}//classes for handling files with no exceptions
Class Fileopenexception extends exception{}//classes 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 "The 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, prompts the user to confirm the readability of the file
{
echo "The program has an exception 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 user-defined exception information
echo "Exception code:". $e->getcode (). " \\n "; Returns the user-defined exception code
echo "File name:". $e->getfile (). " \\n "; Returns the file name of the PHP program 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 passed
echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string
}
function File_open ($path)
{
if (!file_exists ($path))
{
throw new Fileexistsexception ("File cannot be found", 1); Throw Fileexistsexception Exception Object
}
if (!fopen ($path, "R"))
{
throw new Fileopenexception ("File cannot be opened", 2); Throw Fileopenexception Exception Object
}
}
?>
4 re-throwing anomalies to the upper
Class Fileexistsexception extends exception{}//classes for handling files with no exceptions
Class Fileopenexception extends exception{}//classes 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 "The 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, prompts the user to confirm the readability of the file
{
echo "The program has an exception 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 user-defined exception information
echo "Exception code:". $e->getcode (). " \\n "; Returns the user-defined exception code
echo "File name:". $e->getfile (). " \\n "; Returns the file name of the PHP program 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 passed
echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string
}
function File_open ($path)
{
Try
{
if (!file_exists ($path))
{
throw new Fileexistsexception ("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; Throw exception
}
}
?>
http://www.bkjia.com/PHPjc/319361.html www.bkjia.com true http://www.bkjia.com/PHPjc/319361.html techarticle 1 First try,catch? php $path = "D:\\\\in.txt"; try//detect exception {File_open ($path);} catch (Exception$e)//catch Exception {echo$ E-getmessage (); } functionfile_open ($path) {if (!file_ ...