Cytat
Be sure that your exceptions honor layer abstraction.
One of the more complicated things about handling exceptions is that you want to honor layer abstraction when throwing and catching exceptions.
For example, let’s say that PDO raises an exception due to a unique key constraint in the database. Unhandled, it will bubble up to the top. If the PDO exception was caused by something in your Controller, allowing the PDO exception to bubble up would be a violation of layer abstraction.
A better choice would be to catch the PDO exception and wrap it in a Controller exception. For example:
One of the more complicated things about handling exceptions is that you want to honor layer abstraction when throwing and catching exceptions.
For example, let’s say that PDO raises an exception due to a unique key constraint in the database. Unhandled, it will bubble up to the top. If the PDO exception was caused by something in your Controller, allowing the PDO exception to bubble up would be a violation of layer abstraction.
A better choice would be to catch the PDO exception and wrap it in a Controller exception. For example:
<?php try { // some PDO action here } catch(PDOException $pdoE) { throw new ControllerException('There was an error: ' . $pdoE->getMessage() ); }
Cytat
When the exception bubbles up, the PDO exception will have been handled, but the message will be included in a ControllerException. This is an acceptable way to handle exceptions that honors the principle of layer abstraction.
Nie rozumiem po co owijanie wyjątków i wyrzucanie kolejnych. W czym to ma pomóc? I tak na końcu widzę na ekranie (Symfony profiler) komunikat o błędzie i linię z błędem. Co mi daje inna nazwa klasy exception?
Bo, wcześniej se zanotowałem z innych internetów, przypadki ponownego wyrzucania wyjątków (owijania):
- -gdy w mojej bibliotece używam 3rd party library
-..i nie chcę by klient mojej biblioteki widział exceptions z 3rdPL. - - gdy na kolejnych poziomach warstw abstrakcji dodaję nowe fakty.
-..pomocne w ostatecznych naprawieniu błędu
-..pomocne w lepszym zrozumieniu błedu. - -jeśli nie jesteś w stanie naprawić exception na niższym poziomie
-..to re-throw i bąbelkuj w górę.
No ale przykład z bloga mi nie pasuje do niczego z powyższych.