Jump to content

thowing/catching exceptions


heffe6

Recommended Posts

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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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.');
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.