try and catch with a prepared statement

折月煮酒 提交于 2019-12-02 13:21:20

If you wanna track all the errors that happen in your code, you can use try, catch statements to handle them in proper way. Inside try block, you can put all your code or part of it. If something fails, it will generate an exception.

That exception also can be thrown by yourself using the next statement:

throw new Exception("I think something's failing");

Just after try block, you have to declare the catch block which will handle the exception, making it available to you. In the following example I will print the error message.

<?php

try {
    /* all your code */
} catch (Exception $e) {
   /* if something fails inside try block will be catched here */
    print $e->getMessage();
}

You can find more about this here: http://php.net/manual/en/language.exceptions.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!