15

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

    try{
        $vtest = '';
        print(array_pop($vtest));
    }catch(Exception $exx){}

For it to work with array_pop, $vtest should obviously be an array, not a string. Nevertheless, when I run this code the Warning is exhibited. I don't want that, I just want the code to fail silently.

Question: Is there something special about PHP try-catch compared to other languages that cause this not to work?

Disclaimer: Just for reference, it is true there are other ways to handle this situation in PHP, but these are undesirable. The goal here is to avoid:

The "at-sign" trick:

        $vtest = '';
        print(@array_pop($vtest)); // <-- would like to avoid this

Type Casting:

        $vtest = '';
        $vtest = (array)$vtest;  
        print(array_pop($vtest));
dreftymac
  • 31,404
  • 26
  • 119
  • 182
  • **See also:** http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/#error-handling, http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning, https://stackoverflow.com/questions/7116995/is-it-possible-in-php-to-prevent-fatal-error-call-to-undefined-function, https://stackoverflow.com/questions/1241728, https://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning, https://stackoverflow.com/questions/841500/php-exceptions-vs-errors – dreftymac Apr 02 '15 at 23:12

4 Answers4

14

Warnings and notices are not technically exceptions in PHP. To catch an exception it has to be explicitly thrown, and many of the built-in libraries of functions do not throw exceptions (mostly because they were written before PHP supported exceptions).

It would have been nice if somehow exceptions were built on top of the existing notice/warning/error framework but perhaps that is asking too much.

Matt Bridges
  • 48,277
  • 7
  • 47
  • 61
  • The question was "Is there something special about PHP try-catch compared to other languages that cause this not to work?" I think it's a completely fair answer to that question. And for the record I love PHP and it's my language of choice for server-side web development. – Matt Bridges Jul 06 '09 at 14:58
  • I wasn't disagree with you ;), and I see the direction your answer is taking now. :) – MitMaro Jul 06 '09 at 15:07
14

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');
Lawrence Barsanti
  • 31,929
  • 10
  • 46
  • 68
2

You can catch such errors when you convert every error to an exception. I have set up a little error-handling environment. Just test it - it will work.

Community
  • 1
  • 1
eisberg
  • 3,731
  • 2
  • 27
  • 38
0

The only way I can think of would be to do the following:

$vtest = '';
try{
    if(!is_array($vtest)){
        throw new NotArrayException();
    } 
    print(array_pop($vtest));
}catch(NotArrayException $exx){}

Of course if you just want to do this silently you could just do the following since you don't need to catch any exception:

    $vtest = '';
    if(is_array($vtest)){
        print(array_pop($vtest));
    }
Martin
  • 22,212
  • 11
  • 70
  • 132
MitMaro
  • 5,607
  • 6
  • 28
  • 52