1

In the distribution code of pset7, there is a file functions.php containing the function query().

In this function there is a try block followed by a catch block; what purpose do they serve?

6
  • try-throw-catch blocks (in any language) are used to try some code, if any error occurs in executing that code, then instead of allowing the compiler/interpreter to stop execution and output errors, it is preferred to make the program run on its own. So as soon as any error occurs, the code snippet in catch block starts executing instead of making the program to terminate. In this way, programs can be made to run flawlessly even when runtime errors occur. In the pset7 code snippet, try contains something that tries to connect to db, if it fails due to any reason then catch block is executed.
    – sinister
    Aug 31 '14 at 6:48
  • php-exception-handling
    – sinister
    Aug 31 '14 at 6:49
  • Can't we use simple if-else statements rather than try-catch thing? If not, why? Thank you @sinister Aug 31 '14 at 7:05
  • Almost no, they provide additional functionality than if-else. In if-else, you need to figure out the reason of the error and you won't allow the execution to reach the code snippet unless all corner condition are eradicated. try-catch will handle runtime errors, even you don't have to worry which errors(in some cases). The best advice I can provide is that first learn try-catch and then you will get that not all things that can be accomplished through try-catch, can be done through if-else.
    – sinister
    Aug 31 '14 at 7:24
  • 1
    @sinister Those comments definitely would have been worth making into an answer
    – Air
    Sep 5 '14 at 15:55
2

An exception is sort of a tolerable error. When you write some code that might throw an exception, you better enclose it with a try block. If an exception occurred, you may catch it with a catch block.

Basically, catch blocks contain code (usually to display an error message) to be interpreted when the specified exception is caught (aka exception handling).

The try/catch block in query()

try
{
    // connect to database
    $handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
    // ensure that PDO::prepare returns false when passed invalid SQL
    $handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
}
catch (Exception $e)
{
    // trigger (big, orange) error
    trigger_error($e->getMessage(), E_USER_ERROR);
    exit;
}

basically tries to connect to a database using the given information (e.g., the name of the database, the username, etc). If the connection to the database was failed, this code throws an exception.

Now, how do I know that? Well, looking at the manual of PDO, under the Errors/Exceptions section

PDO::__construct() throws a PDOException if the attempt to connect to the requested database fails.

PDOException is an indirect subclass of Exception which is caught by the catch block. If this exception was caught, the code within the catch block is interpreted. If, however, no exceptions were caught, the catch block is ignored and the first statement immediately after it is interpreted.

As for your question

Can't we use simple if-else statements rather than try-catch thing? If not, why?

Well, the try/catch block was made specifically to serve that purpose -- catch and handle exceptions. I'm not sure whether you can handle exceptions using if/else statements, but I'm sure that it's a best practice to handle it using try/catch blocks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .