php異常キャプチャ
15395 ワード
1 try,catch
<?php
$path = "D:\\in.txt";
try //
{
file_open($path);
}
catch(Exception $e) //
{
echo $e->getMessage();
}
function file_open($path)
{
if(!file_exists($path)) // ,
{
throw new Exception(" ", 1);
}
if(!fopen($path, "r")) // ,
{
throw new Exception(" ", 2);
}
}
?>
$e->getMessage() .
2
<?php
$path = "D:\\in.txt";
try
{
file_open($path); //
}
catch(Exception $e)
{
echo " :".$e->getMessage()."
"; //
echo " :".$e->getCode()."
"; //
echo " :".$e->getFile()."
"; // PHP
echo " ".$e->getLine()."
"; //
echo " :";
print_r($e->getTrace()); //
echo $e->getTraceAsString(); // getTrace
}
function file_open($path)
{
if(!file_exists($path)) // ,
{
throw new Exception(" ", 1);
}
if(!fopen($path, "r"))
{
throw new Exception(" ", 2);
}
}
?>
3 ,
<?php
class FileExistsException extends Exception{} //
class FileOpenException extends Exception{} //
$path = "D:\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) // FileExistsException
{
echo " :".$e->getMessage()."
";
echo " 。";
}
catch(FileOpenException $e) // FileOpenException
{
echo " :".$e->getMessage()."
";
echo " 。";
}
catch(Exception $e)
{
echo "[ ]";
echo " :".$e->getMessage()."
"; //
echo " :".$e->getCode()."
"; //
echo " :".$e->getFile()."
"; // PHP
echo " ".$e->getLine()."
"; //
echo " :";
print_r($e->getTrace()); //
echo $e->getTraceAsString(); // getTrace
}
function file_open($path)
{
if(!file_exists($path))
{
throw new FileExistsException(" ", 1); // FileExistsException
}
if(!fopen($path, "r"))
{
throw new FileOpenException(" ", 2); // FileOpenException
}
}
?>
4
<?php
class FileExistsException extends Exception{} //
class FileOpenException extends Exception{} //
$path = "D:\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) // FileExistsException
{
echo " :".$e->getMessage()."
";
echo " 。";
}
catch(FileOpenException $e) // FileOpenException
{
echo " :".$e->getMessage()."
";
echo " 。";
}
catch(Exception $e)
{
echo "[ ]";
echo " :".$e->getMessage()."
"; //
echo " :".$e->getCode()."
"; //
echo " :".$e->getFile()."
"; // PHP
echo " ".$e->getLine()."
"; //
echo " :";
print_r($e->getTrace()); //
echo $e->getTraceAsString(); // getTrace
}
function file_open($path)
{
try
{
if(!file_exists($path))
{
throw new FileExistsException(" ", 1);
}
if(!fopen($path, "r"))
{
throw new FileOpenException(" ", 2);
}
}
catch(Exception $e) //
{
echo "file_open ";
throw $e; //
}
}
?>