2009.08.06

Category: PHP / Tags: ,

phpの try and catch のサンプル

挙動の確認の為のサンプル
ini_set("display_errors","on");
class a
{
function aa()
{
$bool = false ;
if($bool){
//何かの処理
}else{
throw new Exception('error message');
}
}
}
class b extends a
{
function bb()
{
try{
$a = new a();
$a->aa();
}catch(Exception $e){
throw $e ;
}
}
}
class c extends b
{
function cc()
{
try{
$b = new b();
$b->bb();
}catch(Exception $e){
$this->_catchError($e);
}
}
function _catchError($e)
{
$error = “”;
$error .= “
\n”;
$error .= “Catch error in “. $e->getFile() . “ on line ” . $e->getLine() . “
\n”;
$error .= $e->getMessage() . “
\n”;
$error .= “trace
\n”;
$trace = $e->getTrace();
foreach($trace as $key => $val){
$error .= ““. $val['file'] . “ on line ” . $val['line'] . “
.” in class = {$val['class']}”
.”, function = {$val['function']}”
.”, type = {$val['type']}”
.”, function ={$val['function']}”
// .”, args = ” . print_r($val['args'])
. “
\n”
;
}
print_r($e);
print $error ;
       // mailが飛べば本番も安心だね
// mail(’admin@ecamlpe.com’,'php error’, $error);
}
}
$c = new c();
$c->cc();