Welcome Guest, Not a member yet? Register   Sign In
Graceful error handling
#2

[eluser]Damien K.[/eluser]
Some people live by throwing exceptions everywhere. My preference is to not handle any error exception in the controller. Furthermore, exceptions are only thrown on fatal errors (I've seen it use as a messaging system before). However, in PHP I tend to have a preference not to use exceptions. When I do use exceptions, it will look something like this, in pseudo code:

Code:
class A_model
{
    ...


    function insert($data_value_object)
    {
        try
        {
            // try to insert dvo into database
            // but say, for example, an error occurs
            // (eg, cannot connect to db, primary key already exists, etc.)
        }
        catch ($exception)
        {
            // log error
            // throw exception
        }
    }

    ...
}

class A_controller extends Controller
{
    ...

    function add_item()
    {
        $dvo = get_post_data();  // fill with post data
        $this->A_model->insert($dvo);
    }

    ...
}

As you can see, the controller doesn't try to catch any exception. I would usually catch the exception at the last tier and if caught will display an generic application error page. You can optionally choose to set a handler with set_exception_handler() to handle your uncaught exceptions.

For non-fatal errors, the model will return something else so the controller can gracefully inform the user (eg, "Cannot sign up with specified email address because it already exists.").


Messages In This Thread
Graceful error handling - by El Forum - 09-16-2009, 03:34 AM
Graceful error handling - by El Forum - 09-22-2009, 11:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB