-1

I have this piece of code:


     try 
    {
        foreach($obj_c->getGalleries($db_conn1) as $gallery)
                {
            $gallery->Save($db_conn1);
        }
        $k = 0;
        $testing_the_exception = 15/$k; 
                //settin status to 1...
        $obj_c->set_exec_status(3, 1, $db_conn1);
    }
    catch (Exception $e) 
    {
        //settin status to 3...
    $obj_c->set_exec_status(3, 3, $db_conn1);
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    }   
    unset($obj_c);

The fact is that it should be entering into the catch part, because of the division by zero exception, but instead it is just popping a warning and continuing to set status to 1. Is this the expected behaviour? Thanks a lot in advance.

Schleis
  • 41,516
  • 7
  • 68
  • 87
rrubiorr81
  • 285
  • 1
  • 5
  • 15
  • Something similar(or even the same?) to what you're is discussed [here](http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning) – undefined May 13 '13 at 20:04

2 Answers2

3

This is more a a wholesale solution. Set up error reporting to throw exceptions:

<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

/* Trigger exception */
strpos();
?>  

This is an example from the set_error_handler documentation on php.net

Orangepill
  • 24,500
  • 3
  • 42
  • 63
1

Thats because division by 0 is a warning in php and not an exception. The best way to catch this is to test for it and throw your own exception.

if($k == 0)
    throw new Exception("Division by zero");
$testing_the_exception = 15/$k; 
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
  • Thanks a lot people. the fact is that this was only a test, wile I really need to see if the (for example) --$gallery->Save($db_conn1)-- is throwing an exception. In that case I must change the status --in $obj_c->set_exec_status(X, 3, $db_conn1)-- to 1 or 3, depending if an exception was triggered. How else can I know if I've had an error in that iteration. Thx again. – rrubiorr81 May 13 '13 at 20:08