PHPのExceptionってあんまり活用してなかったけど使える。
try{
// めっちゃいろいろやる処理
}catch(Exception $e){
echo $e->getMessage();
}
これが標準な使い方だけど、
めっちゃいろいろやる処理の中で何が起きたかを分岐してとりたいときはこうするといい。
class PageNotFoundException extends Exception{}
class SqlErrorException extends Exception(){}
try{
// めっちゃいろいろやる処理
// if(!$is_page) throw new PageNotFoundException();
// if($mysql_error) throw new SqlErrorException();
}caatch(PageNotFoundException $pe){
}catch(SqlErrorException $se){
}caatch(Exception $e){
}
こうやっておけば、例外処理を分岐して使える。
致命的なエラーはログに仕込んだり、ユーザーにはエラー表示見せないようにしたり。
try{
// めっちゃいろいろやる処理
}catch(Exception $e){
echo $e->getMessage();
}
これが標準な使い方だけど、
めっちゃいろいろやる処理の中で何が起きたかを分岐してとりたいときはこうするといい。
class PageNotFoundException extends Exception{}
class SqlErrorException extends Exception(){}
try{
// めっちゃいろいろやる処理
// if(!$is_page) throw new PageNotFoundException();
// if($mysql_error) throw new SqlErrorException();
}caatch(PageNotFoundException $pe){
}catch(SqlErrorException $se){
}caatch(Exception $e){
}
こうやっておけば、例外処理を分岐して使える。
致命的なエラーはログに仕込んだり、ユーザーにはエラー表示見せないようにしたり。