YanoRyuichi.com/
Wiki
Blog
GitHub
Sandbox
開始行:
* カスタム例外 [#ef4457c0]
** MyException [#waa6f58e]
<?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();
$traceList = $this->getTrace();
foreach ($traceList as $idx => $v) {
$file = $idx == 0 ? $this->getFile() : $trac...
$line = $idx == 0 ? $this->getLine() : $trac...
$class = isset($v['class']) ? $v['class']...
$func = isset($v['function']) ? $v['functio...
$buf[] = sprintf('- %s%s, %s, %d', $class, $...
}
$begin = array_pop($traceList);
$buf[] = sprintf('- {main}, %s, %d', $begin['fil...
return implode("\n", $buf);
}
}
** 使用例 [#hcc82fdc]
*** foo.php [#kafa2aef]
<?php
function fooErr() {
barErr();
}
*** bar.php [#k7b520d4]
<?php
function barErr() {
$baz = new Baz;
$baz->bazErr();
}
*** Baz.php [#iba5c948]
<?php
class Baz {
function bazErr() {
throw new MyException('hello', 'world');
}
}
*** test.php [#lb556800]
<?php
include('foo.php');
include('bar.php');
include('Baz.php');
include('MyException.php');
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";
}
------------------------------------------------------
php ./test.php
=> hello
=> world
=> /home/taro/tmp/Baz.php
=> 4
=> - Baz::bazErr(), /home/taro/tmp/Baz.php, 4
=> - barErr(), /home/taro/tmp/bar.php, 4
=> - fooErr(), /home/taro/tmp/foo.php, 3
=> - {main}, /home/taro/tmp/test.php, 9
** 参考 [#d5da427e]
- http://php.net/manual/ja/language.exceptions.php
- http://www.php.net/manual/ja/class.exception.php
終了行:
* カスタム例外 [#ef4457c0]
** MyException [#waa6f58e]
<?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();
$traceList = $this->getTrace();
foreach ($traceList as $idx => $v) {
$file = $idx == 0 ? $this->getFile() : $trac...
$line = $idx == 0 ? $this->getLine() : $trac...
$class = isset($v['class']) ? $v['class']...
$func = isset($v['function']) ? $v['functio...
$buf[] = sprintf('- %s%s, %s, %d', $class, $...
}
$begin = array_pop($traceList);
$buf[] = sprintf('- {main}, %s, %d', $begin['fil...
return implode("\n", $buf);
}
}
** 使用例 [#hcc82fdc]
*** foo.php [#kafa2aef]
<?php
function fooErr() {
barErr();
}
*** bar.php [#k7b520d4]
<?php
function barErr() {
$baz = new Baz;
$baz->bazErr();
}
*** Baz.php [#iba5c948]
<?php
class Baz {
function bazErr() {
throw new MyException('hello', 'world');
}
}
*** test.php [#lb556800]
<?php
include('foo.php');
include('bar.php');
include('Baz.php');
include('MyException.php');
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";
}
------------------------------------------------------
php ./test.php
=> hello
=> world
=> /home/taro/tmp/Baz.php
=> 4
=> - Baz::bazErr(), /home/taro/tmp/Baz.php, 4
=> - barErr(), /home/taro/tmp/bar.php, 4
=> - fooErr(), /home/taro/tmp/foo.php, 3
=> - {main}, /home/taro/tmp/test.php, 9
** 参考 [#d5da427e]
- http://php.net/manual/ja/language.exceptions.php
- http://www.php.net/manual/ja/class.exception.php
ページ名: