pdo should i try/catch every query?

i don't have an error on my page, but i need to change my mysqli calls to pdo because i need an prepared statement call where i don't know how much parameters i need to pass and it seems like i can pass an array of parameters with pdo.

i read on this page:

Warning! If your application does not catch the exception thrown from the PDO constructor, the default action taken by the zend engine is to terminate the script and display a back trace. This back trace will likely reveal the full database connection details, including the username and password. It is your responsibility to catch this exception, either explicitly (via a catch statement) or implicitly via set_exception_handler().

i never used pdo before and some tutorials don't point out the fact that i could give away my username/password without knowing.

should i put every query or just the new PDO(); in a try/catch ? is it good practice? should i use the set_exception_handler() over try/catch ?


TL;DR: no

Most of your queries will not produce exceptions.

While your project is in development, the uncaught exceptions will let you find errors in your SQL code. And when project goes in production, only exceptions that you see should be expected ones. Like "unique key violation", some foreign key restrictions.

Basically, in production code, you should have only try-catch blocks surrounding queries, where exception is part of the expected logic.

链接地址: http://www.djcxy.com/p/26622.html

上一篇: JavaScript错误处理异步函数传递给reduce

下一篇: 我应该尝试/捕获每个查询吗?