TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Handling Exceptions (http://www.talkphp.com/absolute-beginners/4685-handling-exceptions.html)

captainmerton 07-04-2009 04:53 PM

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.

zxt3st 07-06-2009 10:53 AM

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! :)

captainmerton 07-06-2009 11:40 AM

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 07-06-2009 11:45 AM

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?

CoryMathews 07-06-2009 01:15 PM

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



captainmerton 07-06-2009 07:03 PM

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?

Salathe 07-06-2009 07:10 PM

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.


All times are GMT. The time now is 09:33 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0