PDO'nun yöntemleri istisnalar atmak?

2 Cevap php

Yeni bir PDO db işleyici kurarken, ben kullanıcıya tüm db erişim verileri yazdırmak istiyorum bir hata iletisini önlemek için bir try-catch içine her şeyi sarmak lazım.

Ama nasıl Örneğin exec gibi tüm diğer yöntemlerle (), yaklaşık? Ben bir try-catch bloğu içine tüm bu sarmak gerekir? Hangi noktada bir yöntem bir istisna atar söylüyorum PHP belgelerdir?

2 Cevap

Her şeyden önce, hatalarının PDO::setAttribute method, to set the PDO::ATTR_ERRMODE (error reporting) seçeneğini kullanarak, PDO tarafından ele nasıl ayarlayabilirsiniz.

Özellikle, bu yüzden bir hata olduğu zaman bunun yerine bir "hata" raporlama, istisnalar atar -- that's what I generally do. PDO'yu yapılandırmak mümkündür


Then, when a method can throw an exception, it should be indicated in it's documentation -- generaly, it's in the "Return value" section.

Örneğin, PDO::prepare can throw an exception -- depending on error reporting (see what I wrote just before),

If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).


As a sidenote : if you find a function / method that throws an exception, and it's not indicated in its documentation, it might be a good idea to create a bug report (see http://bugs.php.net/ ), so that problem is corrected ;-)

(Errors / mistakes / missing informations in the documentation are treated via the bug-tracker, like any other bug)

Yöntem kılavuzlarına bakarak istisnalarını atar olmadığını görebilirsiniz.

Sizin için kılavuzunu bakarsanız __construct bunu bir istisna atar Hatalar / İstisnalar altında alt görürsünüz.