Intermediate Coding Techniques / Error Capture: Custom Error Handlers pt. 2
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now you may notice that the sequence in which the arguments are given under the trigger_error function is the opposite way around from the way that we have to take arguments in this function. This is confusing; I don't know a good reason why this is the case but as long as we remember that we have to, when using the trigger error function, put the two arguments the other way around from the way round they are in our custom error handler, then we’ll be o.k. Let’s save our script and go over to our browser and see how this is all working out. Let’s enter an invalid email address and we'll submit the form and see what happens. As we can see, our trigger error function has generated an error and because we've specified ‘e_user_error’, it's been a fatal error, so the hyperlink was shown and the script terminated. So far so good, let’s take a look if we put in a valid address or supposedly valid address at what will happen when we hit the deliberate error that we introduced right at the beginning of this, of the last movie rather. What will happen when it hits the database connection variable that is empty let’s have a look. This will not do what we probably hope it will do, in fact it throws up two different error messages and then it shows the ‘thank you’, we have added your comments to the database so, it’s not doing what we want it to at all. Now this is very useful for us as programmers because we’re getting to see the undefined variable error and we’re also getting to see that exactly why that was a problem, that the argument was not a valid connection to my S.Q.L. The reason that all three of these appeared was that in our script, we didn't actually specify that any particular error should be thrown up by this line. We just let php automatically invoke this function with a different error number than either of the two error numbers we’ve selected here to cause a fatal error. So we’re faced with a choice here, either we keep on with this system of error handling, which allows for some errors and ignores others essentially or allows the scripts to continue, I should say while simply giving an error message or we can take out this condition all together and make every error that is generated a fatal error for the purpose of our custom error handler. Now this is less useful for us as programmers but it may well be more useful in terms of stopping the users seeing things that we don't want it to see when an error happens. So if we go back to our browser, resubmit the data, as we can see since every error is treated as a fatal error because we have left out the condition, the invalid connection variable, the deliberate error that we introduced in the beginning of last movie has done what we wanted it to do and the beauty of this is that we’ve done it all without having to introduce any funny little bits at the beginning or end of this line if there’s an error with this line or if there’s an error with this line, then all of these will generate an error that would include the kind of format that we wanted, in this case the ‘whoops’ at the beginning and the ‘click here to go back’ hyperlink and it would also stop executing the script so that we never reached this part of the script and ended up with an invalid piece of information being given to the user. That wraps it up for that chapter and in our next chapter we’re going to be looking at users authentication sessions and cookies.
Tutorial Information
| Course: | PHP |
| Author: | Joshua Mostafa |
| SKU: | 33332 |
| ISBN: | 1889347787 |
| Release Date: | 2002-03-26 |
| Duration: | 9.5 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 