Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Be sure that your exceptions honor layer abstraction.
porzeczki
post 30.12.2016, 14:49:58
Post #1





Grupa: Zarejestrowani
Postów: 144
Pomógł: 0
Dołączył: 15.09.2016
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


http://www.brandonsavage.net/exceptional-p...-to-exceptions/

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:


  1. <?php
  2.  
  3. try {
  4. // some PDO action here
  5. }
  6. catch(PDOException $pdoE)
  7. {
  8. throw new ControllerException('There was an error: ' . $pdoE->getMessage() );
  9. }

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.

Ten post edytował porzeczki 30.12.2016, 15:05:52
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.06.2025 - 12:12