Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw exception without halting execution?

I've dug myself into a bit of a hole with the project I'm working on. I've got this massive project (100s of thousands of lines), and there are MySQL errors everywhere. I'm in the process of cleaning them up now.

I've got this database class which all the queries go through, so what I've done is whenever there is an SQL error, I throw an exception now. The problem is that I can't have the execution stop. It has to continue as it always had, and just log the exception so I can track them down and fix them one at a time.

I was hoping set_exception_handler would do what I want, but the docs specifically say it will halt execution after calling my handler. So how do I get around this?

The exception can halt the current function, but then I want it to drop out of the function, perhaps returning null or false, and then continue as normal, but I need it to call my global exception handler.


To clarify:

I want to throw an exception from my database class (whenever there is an SQL error). I then want to log this error and/or display a message on screen until I can either fix the SQL error, or wrap the offending line in a try/catch. I don't want it to halt execution. If I simply call some error_handler() function rather than throwing an exception, then I can't catch it. If I catch it immediately (also within the DB class), then I can't catch it further down the stack (unless I re-throw it, but then we're back to halting execution).

like image 911
mpen Avatar asked Nov 14 '22 09:11

mpen


1 Answers

If I have understood you correctly:

function myFunction($params) {

    try {

        //your code which throws Exception

    } catch (Exception $e) {

        myErrorFunction($e);
        return false;
    }

}
like image 158
Ingmar Boddington Avatar answered Nov 16 '22 03:11

Ingmar Boddington