![]() |
exception vs die
Hi
Well I have never used exceptions, but see some people do, so what is the main difference between those exceptions and die() or echo() and exit() ?? |
From my viewpoint:
exceptions are the way to go. Why? Because it's the right way to debug your application if something goes wrong. Let's use die() for example - it just stops your script. You have to put some code which you want to print out as your debug info. Exceptions - insystem function that prints the error if any occured on specific part of your code. Example: PHP Code:
Code:
12The second example: PHP Code:
You must use ony numerical values. So the bottom line: Error handling is improved. You can assign your custom error message if there is an error, and you user try{} catch{}. For me it's the way to go. Can't wait what the others have to say :-D Please correct me if I was wrong about something. |
So the execution will go directly to
PHP Code:
|
Yes.
Let's say you have a multilingual site. PHP Code:
|
ok I see :)
Starting to understand it :-) |
The exceptions are not a way of ending your script. die and exit are made for that. Exceptions are used to treat various cases and act accordingly. For example:
Code:
function do_division($a, $b) |
What about makes using exceptions better than using trigger_error or overwriting that with your own error handler using set_error_handler or calling your own error function?
To me exceptions aren't worth it since you have to use try/catch blocks when I could just use a trigger_error or something similar right where I want the error to be spit out. I guess if a function or something could return different types of errors then setting up multiple catch blocks would be good. But then again there's ways to do that without using exceptions. |
For all questions regarding the advantages and disadvantages of using Exceptions, I refer you to PHP5 Exception Use Guidelines :) Although it is targetted at PEAR developers it is well worth a read, particularly for folks who are new to Exceptions.
Alan |
| All times are GMT. The time now is 04:33 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0