how to handle exceptions/errors in php?

Posted by fayer on Stack Overflow See other posts from Stack Overflow or by fayer
Published on 2010-05-09T17:47:36Z Indexed on 2010/05/09 17:58 UTC
Read the original article Hit count: 218

Filed under:
|
|

when using 3rd part libraries they tend to throw exceptions to the browser and hence kill the script.

eg. if im using doctrine and insert a duplicate record to the database it will throw an exception.

i wonder, what is best practice for handling these exceptions.

should i always do a try...catch?

but doesn't that mean that i will have try...catch all over the script and for every single function/class i use? Or is it just for debugging?

i don't quite get the picture.

Cause if a record already exists in a database, i want to tell the user "Record already exists".

And if i code a library or a function, should i always use "throw new Expcetion($message, $code)" when i want to create an error?

Please shed a light on how one should create/handle exceptions/errors.

Thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about oop