カスタム例外
MyException
<?php
class MyException extends Exception {
private $message2 = '';
function __construct($mesg, $mesg2) {
parent::__construct($mesg);
$this->message2 = $mesg2;
}
function getMessage2() {
return $this->message2;
}
function getPretty() {
$buf = array();
foreach ($this->getTrace() as $v) {
$class = isset($v['class']) ? $v['class'] . '::' : '';
$func = isset($v['function']) ? $v['function'] . '()' : '';
$buf[] = sprintf('- %s%s, %s, %d', $class, $func, $v['file'], $v['line']);
}
return implode("\n", $buf);
}
}
使用例
function fooErr() {
varErr();
}
function varErr() {
$baz = new Baz;
$baz->bazErr();
}
class Baz {
function bazErr() {
throw new MyException('hello', 'world');
}
}
try {
fooErr();
} catch (Exception $e) {
print $e->getMessage() . "\n";
print $e->getMessage2() . "\n";
print $e->getFile() . "\n";
print $e->getLine() . "\n";
//print_r($e->getTrace());
print $e->getPretty() . "\n";
}