0

I'm using CodeIgniter and am trying to execute code in a try/catch block with the idea that errors will stop execution of the code after the error until the catch block is reached, as you would normally think it would work.

However on encountering PHP Errors, the code is continuing. This is causing a database transaction complete command to execute which is .... very bad if there's an error and all of the instructions weren't carried out properly. For example, I have this code which is executed in an ajax request:

// start transaction
$this->db->trans_start();   

try {

    $this->M_debug->fblog("firstName=" . $triggerOpts->{'firstXXXName'});

    $data = array("test_col" => 123);
    $this->db->where("id", 4);
    $this->db->update("my_table", $data);

    // if got this far, process is ok
    $status = "process_ok";            

    // complete transaction
    $this->db->trans_complete();    

} catch (Exception $ex) {

    // output the error
    $this->M_debug->logError($ex);

}

In this code, I'm trying to execute a database update as part of a transaction. My call to $this->M_debug->fblog() is designed to just log a variable to PHP Console, and I've deliberately tried to log a variable that does not exist.

This causes a PHP error, which I guess is a fatal error, and the desired result is that the code after the log commands fails, and the transaction does not complete. However after this error, despite reporting the PHP error in Chrome console, the code keeps right on executing, the database is updated and the transaction is completed. Would appreciate any help in how i could stop this from happening.

Thanks very much, G

EDIT --

As requested heres fblog(), it's simply a Chrome console log request of a variable

public function fblog( $var ) {
    ChromePhp::log( $var );
}    
5
  • The catch block is only executed if an exception is thrown, not an error. Are any of the functions within the try block set up to throw an exception on error? Commented Oct 23, 2018 at 13:03
  • If you post your code from fblog() method in the M_debug class I can try and help you. Commented Oct 23, 2018 at 13:05
  • stackoverflow.com/a/25504983/8121583 Commented Oct 23, 2018 at 13:06
  • Thanks for your response Adam. I posted fblog() as requested, its very simple. So if i understand i need to manually throw an exception on error. But how do i detect the error to throw the exception? I like the idea of just running everything in a try block because you don't know what is going to cause the error. Commented Oct 23, 2018 at 16:30
  • Also maybe you should start the transaction in the try block as well? Not sure if you've solved your problem but just a thought. Commented Oct 24, 2018 at 16:24

1 Answer 1

1

Assuming you're using PHP 7.0 or higher, you can catch PHP errors as well as exceptions, however you need to catch Error or the parent Throwable type rather than Exception.

try {
    ...
} catch (Throwable $ex) {
    //this will catch anything, including Errors and Exceptions
}

or catch them separately if you want to do something different for each of them...

try {
    ...
} catch (Exception $ex) {
    //this will catch Exceptions but not errors.
} catch (Error $ex) {
    //this will Errors only
}

Note that if you're still only PHP 5.x, the above won't work; you can't catch PHP errors in older PHP versions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. I tried changing Exception $ex to Throwable $ex and it had no effect, the code is still executing and nothing is being caught. Am I doing something wrong still? thanks
You didn't really say what kind of error you're getting; that might be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.