Jump to content

Why exception not just use die() or exit()?


Tertius

Recommended Posts

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

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.