Is die() dead, and ONLY use exceptions?

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
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Is die() dead, and ONLY use exceptions?

Post by jeff00seattle »

Hi

I have been told that since the advent of exception handling within PHP, that nobody should never ever use die(), trigger_error(), and set_error_handler() any more. Is this a true statement. Is this an expectation within PHP coding standards?

That all error handling should be performed using only exceptions and custom exceptions.

Advise

Thank

Jeff in Seattle
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is die() dead, and ONLY use exceptions?

Post by requinix »

People using frameworks should use exceptions. People not using frameworks should still try to use exceptions, but sometimes a die() is okay.

As for not using trigger_error and set_error_handler, I totally disagree. Sometimes I want to raise an error without raising an exception.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Is die() dead, and ONLY use exceptions?

Post by Eran »

die() and exceptions do different things and are not interchangeable.
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

Re: Is die() dead, and ONLY use exceptions?

Post by jeff00seattle »

tasairis wrote:People using frameworks should use exceptions. People not using frameworks should still try to use exceptions, but sometimes a die() is okay.
Since exceptions are available, then when is die() is okay?

Thanks for your reply
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is die() dead, and ONLY use exceptions?

Post by requinix »

Exceptions let you interrupt control flow to respond to an error. Maybe you don't need to.

Maybe you don't need to display a fancy error to the user. Maybe a simple error message will suffice. Maybe all you need is a simple application and a user-friendly response would give no added benefit.
Post Reply