Using Exceptions for Form Errors/Messages

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Using Exceptions for Form Errors/Messages

Post by anjanesh »

Hi

Im thinking of using a specific exception class for form errors generated by users - like mandatory input field left empty etc.

Code: Select all

class Ex_User extends Exception
 {
        public function __construct($message)
         {
                parent::__construct($msg);
         }
 }

try
 {
        //
 }
catch (Ex_MySQL $e)
 {
        //
 }
catch (Ex_User $e)
 {
        $msg = $e->getMessage(); // Display this msg in the html page as an error message to the user
 }
catch (Exception $e)
 {
        //
 }
But I remembered that Exception stores filename and linenumber and has stack tracing routines etc, exceptions are mainly designed for development errors, not for html-form messages.

Is it a bad idea to use an Exception for this purpose ?

Thanks
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You can use exceptions for any form of error handling, it is down to you (the developer) if the end user should see the full stack trace or not..

Code: Select all

catch (Exception $e) {
    echo "There has been an exception!"; // stack trace is not displayed..
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Although it's feasible to pop the error message into an exception it may be a little to abrupt. What if the user filled in more than one field incorrectly for example? Another way people deal with this is to setError($field, $message) in the $request object (or somewhere else that makes sense). Then you can just check if $request->hasErrors(), display them.
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

I'm more of the philosophy that "Exceptions should be EXCEPTIONAL!" Does that make sense?

I use exceptions a fair amount but those exception rarely if ever get thrown. And when they do, there is something very wrong going on like a database server is down or the OS is acting up. Things that I could really care less about the performance hit I'm taking for throwning the exception.

I ONLY use them to catch programming problems, not user error problems. They do come with quite a bit of overhead. And if your using them to catch user mistakes and forget to catch one, your user is left looking at a really ugly mess they should never have been exposed to.

Also as pointed out, you can paint yourself into a corner with exceptions if you need to keep track of multiple errors the user might have caused.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

What's nice about exceptions, however, is how they rewind the execution stack, when you otherwise would have needed to bubble up the error with special return values, etc. User errors, however, often change the execution flow of the program, so you need tight integration between the model and controller in these cases.
Post Reply