dougan7657 2012-03-10 00:09
浏览 133
已采纳

异常处理程序

I have to develop an exception handler that should handle like 5 different type of exceptions. Let's call them simply Ex1, Ex2, Ex3...

I though of doing a single class called ExHandler which will be instantiated like this:

...
} catch (Ex1 $e) { $h = new ExHandler($e); $h->render(); }
catch (Ex2 $e) { $h = new ExHandler($e); $h->render(); }
catch (Ex3 $e) { $h = new ExHandler($e); $h->render(); }
...

And inside ExHandler manage each different Exception differently using $e instance of Ex1, $e instance of Ex2, $e instance of Ex3...

But It doesn't seems a very good practice to me. Is it good? Is there any other way of doing this? Should I create an Ex1Handler, Ex2Handler, Ex3Handler...? My S.O.L.I.D spirit tells me something is just wrong here. What is it?

  • 写回答

1条回答 默认 最新

  • dongshi6969 2012-03-13 21:42
    关注

    I need to note before I answer this, that procedural programmers will look at this and think it's dumb :) but I can live with that, this is assuming an OOP application with HTML templating that outputs after the output_buffer is cleaned.

    I always create a try/catch block encompassing the majority of my code in one call usually at the point where I start requiring other files as well as starting an output_buffer whilst in development.

    ob_start();
    
    try {
        switch($appPage) {
            case('work'):
                require_once('im_bored_at_work.php');
                break;
            case('home'):
                require_once('im_a_little_less_bored_at_home.php');
                break;
            default:
                require_once('on_the_fence.php');
        }
    } catch (Exception $e) {
        // Handle exception caught and apply formatting
    }
    
    $devOut = ob_get_contents();
    ob_end_flush();
    

    To give an example how I would handle the multiple exceptions you need to catch with a custom class

    class CustomExceptionHandler extends Exception {
    
        private $msg;
        private $code;
        private $otherVars;
    
        public function __construct($msg,$code=0,$otherDebugVar=null){
            $this->msg = $msg != null ? $msg : "An unknown exception was thrown";
            $this->code = $code;
            $this->otherVars = $otherDebugVar;
            parent::__construct($msg,$code);
        }
    
        public function getOtherVars() {
            return $this->otherVars;
        }
    
    }
    

    The idea is to just keep the custom information within the exception object, and when you rethrow the exception at the end of a try/catch block as a standard exception you include the formatted custom message, it shouldn't really matter now which Exception handler picked up the original exception as all the info you will need will come downstream and be caught in the original try / catch block.

    class BasicTemplate {
        private $template;
        private $path;
        private $contents;
    
        public function __construct($template, $path) {
    
            $this->template = $template;
            $this->path = $path;
            $this->buildTemplate();
        }
    
        private function buildTemplate() {
    
            if ($contents = @file_get_contents($this->path . $this->template)) {
                $this->contents = $contents;
            } else {
                $e = new CustomExceptionHandler("Message",2,$this->path . $this->template);
                // Do whatever else you want to do with custom exception handling class
                throw $e;
            }
        }
    }
    

    Now you need to catch your exception and rethrow it:

    try {
        $html = new BasicTemplate($temp,$path);
    } catch {CustomExceptionHandler $e) {
        throw new Exception("Message: {$e->getMessage()} Other Info: {$e->getOtherVars()}",$e->getCode());
    }
    

    That's the rough idea anyhow, hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题