PHP try-catch blocks: are they able to catch invalid arg types?

前端 未结 4 450
一整个雨季
一整个雨季 2020-11-30 12:37

Background: Suppose I have the following obviously-incorrect PHP:

    try{
        $vtest = \'\';
        print(array_pop($vtest));
    }cat         


        
4条回答
  •  情书的邮戳
    2020-11-30 12:56

    A warning will always be produced by the code you provided but you can use set_error_handler to dictate how the warning is handled; i.e. you can cause it to throw an exception. Furthermore, you can use restore_error_handler to return to default error handling when your done.

    function errorHandler($errno, $errstr, $errfile, $errline) {
        throw new Exception($errstr, $errno);
    }
    set_error_handler('errorHandler');
    

提交回复
热议问题