codeigniter - PHP Exception Handling

558

This is a general question regarding exception handing for exceptions thrown in onther people's code.

I am using the following Codeigniter PHP library: https://github.com/sepehr/ci-mongodb-base-model

which relies upon this library for MongoDB: https://github.com/alexbilbie/codeigniter-mongodb-library/tree/v2

If I call a function in the first library, and it then calls one from the second. Sometimes the second library throws exceptions which I want to be able to deal with in my own code, but there is a try-catch statement around the exception throwing call, which means that it is dealt with before I get a chance to (I just prints the exception to the screen).

My question is:

Without modifying all of the functions in the first and second libraries (i.e. removing all of the try catches), how can I deal with the exception that is thrown?

EDIT

This is how the functions in the second library are arranged:

class SomeClass 
{
    function do_something()
    {
        ...
        try {
            ...
        }   
        catch {
            $this->_show_error('Update of data into MongoDB failed: ' . $exception->getMessage(), 500);
        }
    }

    function _show_error($error_message = '', $response_code = 500)
    {
        //Inbuilt Codeigniter helper function which can be disabled from printing the error to the screen 
        show_error($error_message, $response_code);
    }
}

Although I can disable the error from being printed (which is of course just for debugging), I still have no way of knowing that it occurred and handling it.

299

Answer

Solution:

(should be a comment rather than an answer, but it's a bit long)

just prints the exception to the screen

Really? Are you sure?

Did you check it doesn't trigger an error instead of an exception and you're running this on a system which is not configured as a production server?

If so then I'd steer way clear of this as a library.

(I sincerely doubt anyone would write code that dumb and publish it without lots of warnings)

People are also looking for solutions to the problem: html - Php-Myql: select every row with same specific value

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.