user5297283
user5297283

Reputation:

Understanding try and catch

This probably sounds ridiculous. However, if you don't ask you'll never learn.

I'm relatively new to PHP and self-taught so I haven't exactly learnt everything "to the book".

Is the following required:

try {

}

catch {

}

Am I right in thinking that the try will try to "execute" the code within the brackets and the catch will try and catch the result of the outcome? If there is nothing to catch then it will throw an error?

Upvotes: 0

Views: 138

Answers (6)

Swapon Ahmed
Swapon Ahmed

Reputation: 11

try catch is used for exception handling or error handling.Put your script in try block and write your custom error message in catch block.

    try{
      // put here script
}catch(Exception $error){
   //your custom message
   echo 'Caught exception: ',  $error->getMessage(), "\n";
}

If your script does not execute then it will be jump catch block and access message using $error object.

What is the benefit? The benefit is the whole script will not be stop to execute. It will be continue other block.

Upvotes: 1

Programmer
Programmer

Reputation: 453

Try and Catch is known as Exception Handling

According to w3schools:

Exceptions Handling are used to change the normal flow of a script if a specified error occurs.

For More: http://www.w3schools.com/php/php_exception.asp

Upvotes: 0

Grzegorz Gajda
Grzegorz Gajda

Reputation: 2474

Simple example:

<?php

    class A {

        public function getA($a = 0)
        {
            if ($a === 0) {
                throw new ItCantBeZeroException("Message");
            }

            return $a;
        }
    }

    // I want to throw default exception because I'm not sure
    // am I doing it right or what can I do with bad parameter.
    $a = new A;
    echo $a->getA(0);

    // Now, I know what I can do if developer write bad input.
    // It can't be 0, so I just print my custom error message
    // to my page.
    try {
        $a = new A;
        echo $a->getA(0);
    } catch (ItCantBeZeroException $e) {
        echo "Parameter can't be zero. Try again.";
    }
?>

You can define your own exceptions (like ItCantBeZeroException). Exceptions throw error on site (like "Message") but we can catch them and change to something we want.

  1. You write simple class where some code must be string or integer between 0 and 20.
  2. You use this code, but when user make variable 21, simple class throw error.
  3. You refactor code to catch exception and try to fix code, e.g. change any integer greater than 20 to 20. Then code works properly.

Upvotes: 0

Sandeep Kumawat
Sandeep Kumawat

Reputation: 81

Try block is hold the code which you want to execute. and Catch block is hold the code if you have cause any error then it will execute the catch code or error message.

Basically try and catch we are using for the error handling and avoid to break the control flow of the program and page.

Upvotes: 0

ʃʈɑɲ
ʃʈɑɲ

Reputation: 2684

In the try block you execute code, whenever something fails in that block it will jump to the catch block. You usually define a variable holding the exception. So to answer your question, no it will not process the catch block when there is nothing going wrong in the try block. (unless you specifically throw an exception)

try {

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Upvotes: 0

Jonathan Lam
Jonathan Lam

Reputation: 17361

The first assumption is correct: the code in try will be attempted to run.

However, if no error is thrown, then the block exits normally. If there is an error thrown, then the try execution ends early and goes into the catch block. So your second idea is switched.

Upvotes: 2

Related Questions