Exceptions are often overlooked because of the extra setup you need to do in order to create a good design even though I myself am not a guru of any object oriented design principles. Nevertheless, a programmer should take advantage of its capabilities.
Here is a simple way of using an extended exception in php.
$franz = 'alien';
try{
if($franz != 'human'){
throw new My_Extended_Exception('Error, you are not a human. Go back to your planet', 306);
}
} catch (Exception $e) {
echo $e->logError()->outputError();
}
The neat part with extending an exception is you can play with the class and do your own logging. As an example.
class My_Extended_Exception extends Exception
{
protected $_errorMessage;
public function __construct($message, $code)
{
parent::__construct($message, $code);
}
public function logError()
{
$this->_errorMessage = $this->message . ' code:' . $this->code . ' on line: ' . $this->getLine() . ' in file: ' . $this->getFile() . PHP_EOL;
file_put_contents("errors.txt", $this->_errorMessage,
FILE_APPEND | LOCK_EX
); return $this;
}
// sends the error to the browser;
public function outputError()
{
if($this->_errorMessage){
return $this->_errorMessage;
}
}
}
A little about the code. By extending our exception we added a little ability to log our error messages then also output it to the browser. We basically automate the process and should have control on our errors.
Another big plus using an exception is the native methods of the error class. Here I used 2 which is getLine() and getFile(). Pretty self explanatory which will get the line number of the error and also the file where it came from. There are more of these methods in php.net documentation. I also passed the $message and $code argument to the parent class which is Exception in order to access the message and code property directly (eg: $this->message).
And thats it! hope it helped some.
11 comments:
celine handbags, chi flat iron, nike air max, nfl jerseys, ugg boots, lululemon, mcm handbags, marc jacobs, supra shoes, soccer shoes, lancel, beats by dre, vans, p90x3, ghd, herve leger, nike free, ferragamo shoes, burberry, birkin bag, juicy couture outlet, montre pas cher, soccer jerseys, ralph lauren, hermes, north face outlet, ugg boots, valentino shoes, louis vuitton, jimmy choo, new balance shoes, mulberry, rolex watches, louis vuitton, abercrombie and fitch, oakley pas cher, reebok outlet, hollister, air max, mont blanc, bottega veneta, converse shoes, ray ban pas cher, karen millen, wedding dresses, hogan, yoga pants, insanity, asics gel, timberland
2015-09-16 zhengjx
Michael Kors Outlet Stores Online
canada goose outlet
Kobe Shoes,Kobe 9,Kobe 8 Discount
Coach Factory Outlet Official Coach Handbags Sale
ray-ban sunglasses
Louis Vuitton Handbags Outlet Online
michael kors bag
Coach Outlet Online Coach Factory Outlet
Louis Vuitton Handbags UK For Sale
Louis Vuitton Handbags Official USA Website
timberland boots
ugg boots
Oakley Sunglasses Official Website Cheap Off
coach factory outlet online
nike trainers
toms outlet
celine handbags
michael kors outlet
Hollister UK Clothing Store
Louis Vuitton Outlet Discount Handbags
Cheap Louis Vuitton Handbags Luggage Bags Sale
cheap jordans
hollister uk
michael kors bags
ugg boots sale
Gucci Outlet Online 80% OFF
ugg australia
michael kors outlet
Mont Blanc Pens For Sale
Michael Kors Outlet Online 70% Off Handbags
ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun
chenlina20151127
michael kors outlet
canada goose
louis vuitton outlet
north face outlet
cheap jordan shoes
abercrombie kids
michael kors handbags
coach factory outlet
hollister
air max 90
air max uk
michael kors outlet online
cheap oakley sunglasses
michael kors outlet online
polo ralph lauren outlet
michael kors outlet
christian louboutin outlet
michael kors outlet
kids lebron shoes
prada handbags
coach factory outlet
ugg sale
coach outlet
cheap oakley sunglasses
cheap jordans
cheap uggs for sale
cheap uggs
hollister uk
fitflops
ugg boots
michaek kors handbags
tiffany and co
tod's shoes
true religion outlet
michael kors outlet
louis vuitton outlet
ugg sale
jordan 11s
cheap ray ban sunglasses
michael kors outlet
as
Here we present to introduce to all visitors in this great blog. and thank you for allowing me .
cara menggugurkan kandungan
obat aborsi
tanda tanda kehamilan
Aol Technical Support Phone Number
Aol Technical Support Number
Aol Tech Support Number
Aol Tech Support phone Number
Aol Support Phone Number
Aol Tech Support Number
Outlook Technical Support Phone Number
Outlook Technical Support Number
Outlook Tech Support Number
Outlook Tech Support Phone Number
Outlook Support Phone Number
Outlook Tech Support Number
Quicken Support Phone Number
Quicken Support Phone Number
Quicken Support Number
Quicken Tech Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Number
Quicken Technical Support Phone Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Tech Support Number
This is a very good article material and it is very useful for us all. thank you . cara menggugurkan kandungan
شركة دار الصيانة
شركة جلى بلاط بجدة
شركة مكافحة حشرات بالباحة
شركة مكافحة حشرات بتبوك
شركة تنظيف بتبوك
Nice blog. Thanks for sharing this post.
Clipping Path Services
Thankyou For Sharing Such An Excellent Post Enjoyed Reading it.
Regards
microsoft365.com/setup
micrsoft365.com setup
microsoft365
office365.com
www.primevideo.com/mytv
primevideo.com/mytv
123.hp.com/setup
kashmir honeymoon packages
best kashmir holiday packages
Post a Comment