TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-17-2009, 08:57 AM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Exception Handling

I am using the generic php5 Exception class to "catch" errors in my script. I have the code i want to trap errors from within the "try" clause. However while this is useful for displaying errors I want to perform different code before display of error depending on whether i've caught an error or not. can someone advise what the code would be to do this?

eg.

try {

Do some stuff

If error-caught {
do some error related stuff
}

catch(Exception $error) {
print $error->getMessage();
}
captainmerton is offline  
Reply With Quote
Old 05-17-2009, 09:50 AM   #2 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

If I'm thinking of the same thing, I believe the way to do this would be to use an error handler:

PHP Code:
function custom_error_handler ($e_number$e_message$e_file$e_line$e_vars) {
}

set_error_handler('customer_error_handler'); 
You can then set your error message to whatever you want within that function:

PHP Code:
$m "A major mishap took place in '$e_file' on line '$e_line' with the message '$e_message'."
You can then trigger this custom error in other scripts (so long as you have included this file):

PHP Code:
trigger_error('Could not connect with error: ' mysqli_connect_error()); 
This is the kind of thing that I'm looking into at the minute, and if I've understood your query properly I think this should help you.
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 05-17-2009, 12:20 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Here's the code I am messing with (see below) apologies if it isnt easy to read. What i am trying to achieve is i'm happy with catching the error at the end for example but wonder if i can access the error before the end of the script eg. this script will display a login form if you arent logged in already. If you submit an empty form it throws an error which is displayed at the end of the script. However i want to add some code around the form display part to say well if you have submitted the form but an error has bene thrown back then re-display the form code. I guess what i am asking is do i need the catch(Exception $error) code to access the error so i need to nest try statements? Dunno if i am making myself clear but just trying to understand this i.e. before that last statement "catch(Exception $error)" can i access $error or is the catch statement instatitiating an $error object.

Quote:
try {
require_once('./lib/login.class.php');
$login = new Login;

/* Verify the user has login, otherwise redirect to login page */
if($login->verifyAccess()) {
/* User is logged in, display welcome message */
print "Welcome " . $_SESSION['name'];
require_once('inc.loggedin.php');
} else {

/* Check if the form has been submitted. If not, redirect to login page */
if(!isset($_POST['submit'])) {
/* Include the HTML for the form */
require_once('inc.form.php');
}
else {
$username = $_POST['username'];
$password = $_POST['password'];

/* Verify the login details are correct and redirect to secure.php */
$login->verifyLogin($username, $password);
}
}

}
catch(Exception $error) {
print $error->getMessage();
}
captainmerton is offline  
Reply With Quote
Old 05-17-2009, 12:35 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

My advice would be to not misuse exceptions. They're there to cater for exceptional circumstances: a validation error during the login procedure is something that happens as a matter of course, it is not an exceptional state in my opinion.

If you do want to continue using exceptions in this nature, perhaps focus on capturing specific types of exceptions and have a more narrow focus on the code between the try and catch. Consider catching only LoginValidationException or some such before you fall back to a generic exception.
Salathe is offline  
Reply With Quote
Old 05-17-2009, 01:07 PM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks for the advice. I believe I can extend the class to cover specific types of exception.
captainmerton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error handling Ross TalkPHP Developer Team 0 01-17-2009 10:27 PM
Error Handling and Exceptions Farmer Absolute Beginners 3 12-11-2008 06:41 PM
uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ER Orc Javascript, AJAX, E4X 2 02-26-2008 02:12 PM
Using Exceptions to Provide Better Feedback Wildhoney Tips & Tricks 5 11-19-2007 06:55 AM
File_Get_Contents exception WinSrev General 7 09-25-2007 09:25 PM


All times are GMT. The time now is 05:45 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design