Placement of exception errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Placement of exception errors

Post by synical21 »

Hey PHP Gurus you recently showed me how I can use exception handleing in my web application and it works great, I just got one more problem with errors bugging me now though.

The situation is I have a form which uses exceptions when submitted, all the exception code is at the very top of the document thats including the catch blocks. Now this displays errors on the very top of the page above the template obviously. This is no good as it breaks my template so i need to place the errors in the body part of the document. I have tried putting the catch blocks in the body but then the code inbetween the catch blocks and the new exception is not read so it results in half the template missing.

How can I get the error messages from the catch blocks to display in the body without loosing half the template.

This is the error message code:

Code: Select all

 
    // if successfully insert data into database, displays message "Successful". 
if($sql){
    
echo "Thank you for submiting your job, our team will now take a look and approve very soon ";
echo "<BR>";
echo "$feature";
echo "<a href='/jobs.php'>Click here to go back</a>";
}
 
else {
echo "ERROR";
}
    
  } catch (customException $e)
  {
  echo $e->errorMessage();
  }
  
  catch (workersException $e)
  {
  echo $e->errorMessage2();
  }
        
  catch(Exception $e)
  {
  echo $e->getMessage();
  }     
 
}
}
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Placement of exception errors

Post by requinix »

Store the error message(s) to be printed later.

Code: Select all

$errors = array();
try {
    // ...
} catch (Exception $e) {
    $errors[] = $e->getMessage();
}
 
// ...
 
if ($errors) {
    echo "Errors:\n<ul>\n";
    foreach ($errors as $error) echo "<li>{$errors}</li>\n";
    echo "</ul>\n";
}
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Placement of exception errors

Post by synical21 »

Thank you tasairis.
Post Reply