try and catch with a prepared statement

后端 未结 1 1347
名媛妹妹
名媛妹妹 2021-01-29 07:30

I have had a very hard time with this query and it was suggested to me to learn try and catch blocks to more easily figure out what is wrong. So this is my first attempt at it.<

相关标签:
1条回答
  • 2021-01-29 08:04

    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

    0 讨论(0)
提交回复
热议问题