TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-04-2009, 04:53 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Handling Exceptions

Can anyone offer some advice on how to best handle exceptions thrown by input validation i'm doing i.e. i want to make sure for example a date is in the correct format. I am using the standard php Exception class eg.

throw new Exception("That Date aint right!");

I am "catching" the error later on and outputting it however i want to avoid doing a whole lot more validation checking first but i dont want a whole load of try and catch clasues everywhere. Is there a way i can use if statements to check for the existence of an Exception object and avoid processing further unnecessary code when i already know the input is in error.

eg.

If (no Exception object in existence) {
Do further validation
}

If (no Exception object in existence) {
insert date into database
}

PS. When i get the structure correct i will then start extending the Exception class to be more specific about the type of error.

Any guidance?

Thanks.
captainmerton is offline  
Reply With Quote
Old 07-06-2009, 10:53 AM   #2 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Heya pal! :) here are some article links that we can found here in our community :) have a read at them and they explain and help you a lot.

Using Exceptions - Article
Introduction to debugging and error checking

Goodluck! :)
__________________
Serenity Project - 5% (Layout) - Ongoing....
Project Serenity Free Life!....
zxt3st is offline  
Reply With Quote
Old 07-06-2009, 11:40 AM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks for that. The first thread on exceptions was useful didnt realise that the minute an exception is instantiated then control is passed to whats in the catch clause. Makes me realise that extending the exception class is essential and the use of codes may help a lot also.

Any thoughts/experience of using exceptions still greatly appreciated...
captainmerton is offline  
Reply With Quote
Old 07-06-2009, 11:45 AM   #4 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

One observation I have if my understanding of how exceptions work in PHP is correct is that should I wish to throw several exceptions eg. on validation of input from a form then this would not be possible because as soon as the first exception object was instantiated control would pass back to the "catch" clause (eg. should i want to create exceptions for every input field not correctly populated on the form and then displayed back as exceptions to the user) Surely this cannot be a correct understanding?
captainmerton is offline  
Reply With Quote
Old 07-06-2009, 01:15 PM   #5 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

This sort of seems like overkill to use exceptions. Personally I just use a string.

PHP Code:
if(!validDate) {
  
errorMsg += "problem with date<br />";
}
...
if(
errorMsg == "") {
  
// valid input continue with form
} else {
  
// show error msg for bad input

CoryMathews is offline  
Reply With Quote
Old 07-06-2009, 07:03 PM   #6 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

I agree to a certain extent. Every time someone inputs bad data i am throwing an exception and creating the overhead of instantiating an object when i could just throw out a string. The trade off i assume is I dont have to riddle my code with conditionals saying "if no error keep going" therefore using exceptions and a catch clause i can have far tidier code. Thoughts?

Still can anyone tell me how i create more than one instance of the same exception class without breaking immediatly to the catch clause code on instantiation of the 1st object? is this possible?
captainmerton is offline  
Reply With Quote
Old 07-06-2009, 07:10 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

As soon as any throw statement is made, the execution stops immediately and moves to the appropriate catch statement or an Exception handler if there is one. It just wouldn't make sense to have a scenario where one wants to throw an exception yet keep the current flow of the code going (for more validation checks, as in your example).

Rather than throwing an exception as soon as a validation problem is identified, it sounds like you will want to collect the errors (if there are any) together during the validation process and only after the validation tests are all complete then throw an exception to be caught elsewhere.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling captainmerton Absolute Beginners 4 05-17-2009 01:07 PM
Error handling Ross TalkPHP Developer Team 0 01-17-2009 10:27 PM
Error Handling and Exceptions Farmer Absolute Beginners 3 12-11-2008 06:41 PM
Exceptions and suppressing mysqli errors delayedinsanity Advanced PHP Programming 12 05-07-2008 07:27 PM
Using Exceptions to Provide Better Feedback Wildhoney Tips & Tricks 5 11-19-2007 06:55 AM


All times are GMT. The time now is 02:05 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design