简体   繁体   English

尝试用PHP捕获,我可以这样做吗?

[英]Try catch in PHP, Can I go this way?

I writing a script which has to read content from URL all the time. 我编写的脚本必须始终从URL中读取内容。 Instead of ... 代替 ...

// block 1
Try{
    if(!someAction1){
        throw new exception(someException1);
    }
}catch(Exception $e){
    //exception handling code
}

// block 2
Try{
    if(!someAction2){
        throw new exception(someException2);
    }
}catch(Exception $e){
    //exception handling code
}

// block 3
Try{
    if(!someAction3){
        throw new exception(someException3);
    }
}catch(Exception $e){
    //exception handling code
}

Can I change in to this ... 我可以换到这个......

Try{
    someFunction1()
}catch(Exception $e){
    //exception handling code
}

public someFunction1(){
    if(!someAction1){
        throw new Exception(someException1);
    }

    if(!someAction2){
        throw new Exception(someException2);
    }

    someFunction2()

}

public someFunction2(){
    if(!someAction3){
        throw new Exception(someException3);
    }
}

The reasone I want to do this because there are a lot of try-catch block to create. 我希望这样做是因为有很多try-catch块可以创建。 But all of them only to prevent the script from stopping itself (I'm running it with Crontab). 但是所有这些只是为了防止脚本停止运行(我用Crontab运行它)。 The exception handling code is simple, write the error log file (Same for every try-catch block) 异常处理代码很简单,编写错误日志文件(每个try-catch块都相同)

Yes, you can do that. 是的,你可以这么做。

Just go and try it out yourself. 只是去自己尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM