V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Jaeger
V2EX  ›  PHP

关于 PHP 异常处理,求指教

  •  
  •   Jaeger · Oct 15, 2015 · 2889 views
    This topic created in 3852 days ago, the information mentioned may be changed or developed.
    对 PHP 异常处理不是太懂,请问什么时候该用 Exception 来抛出异常,什么该用 user_error 抛出异常信息,如:在一个函数里面检测到调用者传过来的参数类型不符合要求,是该 throw Exception 还是该 user_error?谢谢了!
    4 replies    2015-10-15 12:33:41 +08:00
    haiyang416
        1
    haiyang416  
       Oct 15, 2015
    以目前的状况来说,你直接使用 Exception 就行了,在新版本( PHP 7 )中 Error 和 Exception 已经统一为一个接口了。
    MrZephyrus
        2
    MrZephyrus  
       Oct 15, 2015
    可以看看这篇文章 http://www.jb51.net/article/30246.htm
    wesley
        3
    wesley  
       Oct 15, 2015
    try{
    //你的代码。。。
    //需要抛出异常的地方:
    throw new Exception($message, $code);
    }catch(Exception $e){//接收异常
    //处理接收到的异常
    }
    surfire91
        4
    surfire91  
       Oct 15, 2015
    面向对象风格的话,建议都用异常
    可以参考下这篇文章 http://www.laruence.com/2012/02/02/2515.html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   790 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 20:15 · PVG 04:15 · LAX 13:15 · JFK 16:15
    ♥ Do have faith in what you're doing.