Bir PHP istisna yürütme durdurma mu?

3 Cevap php
<?php
     function some_function($arg) {
         if($filter->check_for_safe_input($arg)) {
             throw new Exception("Hacking Attempt");
         }
         do_some_database_stuff($arg);
     }
 ?>

Yukarıdaki kod örneğinde, check_for_safe_input başarısız olursa do_some_database_stuff Hiç denilen olsun demek, ya da istisna fonksiyon çalışmasını durdurur? Ben oldukça emin olmamıştım şey, ve genellikle sadece emin olmak için bir başka açıklamada do_some_database_stuff gibi fonksiyonları sopa, ama bu derece iç içe fonksiyonlara ulaşabileceğiniz eğilimindedir.

3 Cevap

Evet, yakalanmamış istisna yazısının yürütülmesini durdurmak ölümcül hatalara neden. Bir istisna atılır Yani do_some_database_stuff function denilen olmayacaktır. Sen istisnalar hakkında daha fazla okuyabilirsiniz this article.

Istisnalar üzerinde PHP kılavuzuna bakabilirsiniz.

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler().

http://ch.php.net/exceptions

So yes, the rest of the function is not being executed, a fata error occurs instead.
If you catch the exception, execution of the script continues in the corresponding catch block, everything "between" the function which throws an exception and the catch block is not executed.

Bir istisna, catched değilse, komut dosyası yürütme sona erecek.

Bkz PHP manual chapter on Exceptions