heffe6 Posted March 18, 2009 Share Posted March 18, 2009 Hello all, I'm new to OOP, I'm having some trouble with exceptions. I've got the hang of throwing and catching them, and even creating my own exception classes, but I'd like to be able to handle the exceptions in a different part of the script then where they occur. For example, at the top of my script, I validate fields and run my sql inserts and what not. If any of those throw exceptions I want to be able to print an error message in my error message div which is further down in the HTML. I'm not able to keep everything in a try block, because regardless of an exception, I still want to print out a lot of the HTML. Here's a representation of what I'm trying to do. <?php - validate form, if it fails, throw exception - run sql, if it fails, throw exception ?> <html> - some data I want to appear regardless - - if there are no exceptions, php data will be echoed here <div id="error_message"> - catch Excpetion - and display relevant error message </div> </html> I have a feeling I'm thinking about this incorrectly? If this is not what exceptions are meant for, can anyone suggest a better way to do this? Thanks, -Jeff Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/ Share on other sites More sharing options...
drisate Posted March 18, 2009 Share Posted March 18, 2009 I think you wana doe something like this <html> <?php if ($_POST){ if ($_POST[bla1]==""){$error=$error."Bla1 is empty!<br>";} if ($_POST[bla2]==""){$error=$error."Bla2 is empty!<br>";} if ($_POST[bla3]==""){$error=$error."Bla3 is empty!<br>";} if ($error==""){ // No error go insert }else{ form($error) } }else{ form(""); } function form($error){ if ($error!=""){ print ('<div id="error_message">'.$error.'</div>'); } } ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/#findComment-787334 Share on other sites More sharing options...
heffe6 Posted March 18, 2009 Author Share Posted March 18, 2009 Yes, thank you, I have done that in the past, but just thought using exceptions might offer more functionality. Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/#findComment-787345 Share on other sites More sharing options...
mrdamien Posted March 18, 2009 Share Posted March 18, 2009 Exceptions are difficult to know when to use. Both of your examples are not good examples of where to use exceptions. For form validation, either the form is valid, or it isn't. If it isn't you should simply show the form again, and ask that it be properly filled. For SQL queries, again, either the query is valid, or it isn't. All the queries you place in your site should be valid 100% of the time, so there is little need to validate them again. Exceptions should more-or-less be used when the script cannot continue past a point, because some external mechanism is not working. This is just my opinion of course. You could of course use exceptions for form-validation, but they won't add any functionality compared to <? if(!isset($_POST['email'])){ $error_message .= "An email is required"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/#findComment-787352 Share on other sites More sharing options...
heffe6 Posted March 18, 2009 Author Share Posted March 18, 2009 So exceptions are primarily used for system errors.. Makes sense. thank you Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/#findComment-787367 Share on other sites More sharing options...
Daniel0 Posted March 18, 2009 Share Posted March 18, 2009 They are used when something "exceptional" occurs, so to speak. E.g. if (!$item = $model->getById($_GET['id'])) { throw new DomainException('Item not found.'); } if (1 > $_GET['page'] || $_GET['page'] > $numPages) { throw new OutOfRangeException('Invalid page number.'); } Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/#findComment-787443 Share on other sites More sharing options...
syed Posted March 18, 2009 Share Posted March 18, 2009 You should really create a validate class to do this. Something similar to this.. $Validate = new Validator(); $Validate->ValidateEmail($_POST['email'], "Email not valid"); $Validate->ValidateEmpty($_POST['name'], "This field is required"); $Validate->ValidateNumber($_POST['mobile'], "Please enter a numeric value for mobile"); if ($Validate->HasError){ print $Validate->ListError(); } A more advanced Validator() class can put error messages next to each field rather than list them. Like always, its just my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/149912-thowingcatching-exceptions/#findComment-787507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.