Tertius Posted February 22, 2009 Share Posted February 22, 2009 can someone tell me Why exception Class not just use die and exit for error handling? when I look at ZCE guidebook p.128, it says , 'Exceptions provide an error control mechanism that is more fine-grained than traditional PHP fault handing, and that allows for a much greater degree of control.' what are those - more fine-grained error control mechanism? - much greater degree of control? for example, the try catch operator is use for catch the excpetion thrown , the inside the catch blocks ,you can do things to handle the exception once thrown, but it is just like I using if else like this. if ($error) { throw new Exception ('This is my error'); } try { if ($error) { throw new Exception ('This is my error'); } } catch (Exception $e) { // } Link to comment https://forums.phpfreaks.com/topic/146299-why-exception-not-just-use-die-or-exit/ Share on other sites More sharing options...
rhodesa Posted February 22, 2009 Share Posted February 22, 2009 it's for more graceful error handling. it makes the most sense when you look at it from the view of a project with multiple people working on it. Bob just got assigned the job of writing a component for a project. The component connects to a database, gets some info, and returns it. Simple enough. Now, John is using the component somewhere else in the project. He calls the component, but there is a problem, the database is down. Now, if Bob's component used die() or exit(), the script would just halt and maybe display some ugly "Database Unavailable" message. Wouldn't it be nice if John could just catch the error and continue on, ignoring that component? Or maybe display his own error message? This is where Exceptions come in. It allows the component to generically say "I found a problem" and then let the script using the component do what it wants with it. Does that help? Link to comment https://forums.phpfreaks.com/topic/146299-why-exception-not-just-use-die-or-exit/#findComment-768148 Share on other sites More sharing options...
Tertius Posted February 22, 2009 Author Share Posted February 22, 2009 yes, it helps me. thanks a lot. Link to comment https://forums.phpfreaks.com/topic/146299-why-exception-not-just-use-die-or-exit/#findComment-768248 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.