Copy Code code 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 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 Not open", 2);
}
}
?>
Copy Code code as follows:
<?php
$path = "D:\\in.txt"; The path where the file resides
File_open ($path); Calling the File_open function
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 Not open", 2);
}
}
?>
Copy Code code as follows:
<?php
function Exception_handler ($e)//functions for handling exceptions
{
echo "Not caught exception:". $e->getmessage ();
}
Set_exception_handler ("Exception_handler"); Setting Exception Handling functions
Try//Detect exception
{
$path = "D:\\in.txt";
}
catch (Exception $e)//Catch exception
{
echo $e->getmessage ();
}
File_open ($path); Calling a function to open a file
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 Not open", 2);
}
}
?>
Copy Code code as follows:
<?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 "FileName:". $e->getfile (). " \ n "; Returns the PHP program filename where the exception occurred
echo "The exception code is in line". $e->getline (). " \ n "; Returns the line number of the line where the exception occurred
echo "Transmission route:";
Print_r ($e->gettrace ()); Returns the route of each step of the trace exception as an array
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, throw an error
{
throw new Exception ("File cannot be found", 1);
}
if (!fopen ($path, "R"))
{
throw new Exception ("File Not open", 2);
}
}
?>
Copy Code code as follows:
<?php
Class Fileexistsexception extends exception{}//classes for processing files that do not have exceptions
Class Fileopenexception extends exception{}//classes for handling file unreadable exceptions
$path = "D:\\in.txt";
Try
{
File_open ($path);
}
catch (Fileexistsexception $e)///If Fileexistsexception exception is generated, prompt user to confirm file location
{
echo "An exception occurred during the run:". $e->getmessage (). " \ n ";
echo "Please confirm the file location. ";
}
catch (Fileopenexception $e)////If Fileopenexception exception is generated, prompts the user to confirm the readability of the file
{
echo "An 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 user-defined exception information
echo "Exception code:". $e->getcode (). " \ n "; Returns the user-defined exception code
echo "FileName:". $e->getfile (). " \ n "; Returns the PHP program filename where the exception occurred
echo "The exception code is in line". $e->getline (). " \ n "; Returns the line number of the line where the exception occurred
echo "Transmission route:";
Print_r ($e->gettrace ()); Returns the route of each step of the trace exception as an array
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 Not open", 2); Throw Fileopenexception Exception Object
}
}
?>
Copy Code code as follows:
<?php
Class Fileexistsexception extends exception{}//classes for processing files that do not have exceptions
Class Fileopenexception extends exception{}//classes for handling file unreadable exceptions
$path = "D:\\in.txt";
Try
{
File_open ($path); Try to open a 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 "FileName:". $e->getfile (). " \ n "; Returns the PHP program filename where the exception occurred
echo "The exception code is in line". $e->getline (). " \ n "; Returns the line number of the line where the exception occurred
echo "Transmission route:";
Print_r ($e->gettrace ()); Returns the route of each step of the trace exception as an array
echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string
}
catch (Fileexistsexception $e)///If Fileexistsexception exception is generated, prompt user to confirm file location
{
echo "An exception occurred during the run:". $e->getmessage (). " \ n ";
echo "Please confirm the file location. ";
}
catch (Fileopenexception $e)////If Fileopenexception exception is generated, prompts the user to confirm the readability of the file
{
echo "An 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, the output error
{
throw new Fileexistsexception ("File cannot be found", 1);
}
if (!fopen ($path, "R"))
{
throw new Fileopenexception ("File Not open", 2);
}
}
?>
Copy Code code as follows:
<?php
Class Fileexistsexception extends exception{}//classes for processing files that do not have exceptions
Class Fileopenexception extends exception{}//classes for handling file unreadable exceptions
$path = "D:\\in.txt";
Try
{
File_open ($path);
}
catch (Fileexistsexception $e)///If Fileexistsexception exception is generated, prompt user to confirm file location
{
echo "An exception occurred during the run:". $e->getmessage (). " \ n ";
echo "Please confirm the file location. ";
}
catch (Fileopenexception $e)////If Fileopenexception exception is generated, prompts the user to confirm the readability of the file
{
echo "An 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 user-defined exception information
echo "Exception code:". $e->getcode (). " \ n "; Returns the user-defined exception code
echo "FileName:". $e->getfile (). " \ n "; Returns the PHP program filename where the exception occurred
echo "The exception code is in line". $e->getline (). " \ n "; Returns the line number of the line where the exception occurred
echo "Transmission route:";
Print_r ($e->gettrace ()); Returns the route of each step of the trace exception as an array
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 Not open", 2);
}
}
catch (Exception $e)//Catch exception
{
echo "File_open function in the process of running an exception";
Throw $e; Throw the exception again
}
}
?>