views:

85

answers:

2

Is it acceptable or sensible to use exceptions to deal with user generated errors? Such as ...

    try
    {
        $job->authorise($user);
    }
    catch (InsufficentCreditException $e)
    {
        return E_INSUFFICIENT_CREDIT;
    }
    catch (PermissionDeniedException $e)
    {
        return E_PERMISSION_DENIED;
    }

or are exceptions reserved for more serious errors only (like file not found, division by zero)?

+6  A: 

Bad user input is never an exception. It's the norm!

Seriously though. Exceptions are for exceptional circumstances, and bad/incorrect data from an external source is usually not an exception.

Mitch Wheat
A: 

I think that the 'InsufficentCreditException' is a good reason to throw if I understand your use case above. All of the positive flow of the program can be made to process top down, any 'exception' to the positive is a failure and therefore an exception.

Mitch says, 'bad user input is never an exception' and 'bad/incorrect data from an external source is usually not an exception', but if you're under 'program by contract', that is exactly the case. What better reason would there be to throw an exception than invalid data received from an outside source?

One final note, your question and your use case don't match. Insufficient Credit is not usually caused by user generated error. A clear case of cause/effect would make this easier to answer.

KevinDTimm