|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
Handling errors in PHP 4 as you all might know has always been a pain in the ass. Along with PHP 5 has comes much better exception handling. When you want to throw an exception you can use code like the following
| <?
try {
echo 3 / 0;
}
catch (Exception $e) {
echo 'Line: '.$e->getLine().'<br>';
echo 'File: '.$e->getFile().'<br>';
echo '<br>Trace: <br>';
echo '<pre>';
print_r($e->getTrace());
echo '</pre>';
}
?> | |
this code will throw an error because you cannot divide by 0, you will see how the information is printed to the screen.
Now you can also extend the Exception class for much more extendability.
| Class DatabaseException extends Exception {
public function __construct($msg) {
parent::__construct($msg);
}
} | |
when you want to throw an error, say you have this
| | $query = mysql_query("SELECT * FROM table") or die(mysql_error()); | |
this is how we would have done this before, now we can do this differently to show a nice message to the user without actually stopping everything, you can show a nice message formatted and fitted for your site design ;)
| | $query = mysql_query("SELECT * FROM table") or throw new DatabaseException('Database Query Failed!"); | |
this code alone is not a complete sample however extending the exception class will allow you to create other methods that you can call to output the design before and after you call the parent::__construct() method.
I hope you guys like this new error handling, any questions use the comment system or weberdev@codebowl.com |
|
| DB Connection Function with error handling and email failure notices Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging | | | A Custom Error Handling And Debugging Class Categories : PHP, PHP Classes, Debugging, Errors and Logging | | | Building a basic error handler with custom error types Categories : PHP, PHP Classes, Errors and Logging | | | [PHP5] PHP Debugger and Helper Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML | | | very simple ftp class Categories : PHP, PHP Classes, FTP | | | PHP Paypal IPN Integration Class v1.0.0 Categories : PHP, PHP Classes, Payment Gateways | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Create HTML forms dynamicly using Javascript & PHP Categories : PHP, PHP Classes, Java Script | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | ElfReader: An ELF (Executable and Linking Format) header information in PHP. Shows how to use the UNPACK function to read data. Categories : PHP, Linux, PHP Classes | | | A simple function to prevent undefined $_POST/$_GET/$_SESSION variable errors Categories : PHP, Variables, Errors and Logging | | | an example of the cyberlib payment class Categories : PHP, PHP Classes, Ecommerce, Credit Cards | | | Power Form Validation Categories : PHP, PHP Classes, Data Validation | |
|
|
|