0

A very simple question from someone without much experience. The following try catch block has the section, "(Exception $e)" : is this like sql, where $e becomes an alias of Exception? If so, is this type of alias assignment used elsewhere in php, because I haven't come across it? I have searched for hours without being able to find an explanation on the web.

function inverse($x) {
 if (!$x) {
     throw new Exception('Division by zero.');
          }
            else return 1/$x;
          }

      try {
            echo inverse(5) . "<br/>";
            echo inverse(0) . "<br/>";
          } catch (Exception $e) {
            echo 'Caught exception: ',  $e->getMessage(), "<br/>";
          }

            echo 'Hello World';
2
  • That's [declaration](en.wikipedia.org/wiki/Declaration_(computer_programming)) Commented Feb 10, 2014 at 15:52
  • @Brian Close, sort of. It looks like one, like what you are used to from other, declarative languages. However that is not true, php has got no concept of explicit declarations. This is a filter, not a declaration. It filters the catch block to catch only those exceptions that match the conditions to be of type Exception (or whatever type). This is why it makes sense to chain multiple catch blocks by specifying different filter expressions. Commented Feb 10, 2014 at 15:55

2 Answers 2

1

What you mention is a filter construction. It resembles a declaration as known from other, declarative languages. However it has a different meaning in fact. Actually php does not have the concept of an explicit declaration (which is a shame...).

Take a look at this example:

function my_func($x) {
    try {
        /* do something */
        if (1===$x)
            throw new SpecialException('x is 1.');
        else if (is_numeric($x))  }
            throw new SpecialException('x is numeric.');
        else return $x;
    }
    catch (SpecialException $e) {
        echo "This is a special exception!";
        /* do something with object $e of type SpecialException */
    }
    catch (Exception $e) {
        echo "This is a normal exception!";
        /* do something with object $e of type SpecialException */
    }
}

Here it becomes clear what the construction is for: it filters out by type of exception. So the question which of several catch blocks is executed can be deligated to the type of exception having been thrown. This allows a very fine granulated exception handling, where required. Without such feature only one catch block would be legal and you'd have to implement conditional tests for potential exception types in each catch block. This feature makes the code much more readable, although it is some kind of break in the php syntax.

You don't have to, but you can create own exception classes with special behavior and, more important, accepting and carrying more information about what actually happened.

Sign up to request clarification or add additional context in comments.

Comments

0

It's OO PHP. The $e is an instance of the exception object.

$e could easily be labelled anything else, so long as it's referred to thereon when you want to getmessages, etc.

For instance;

 try {
        echo inverse(5) . "<br/>";
        echo inverse(0) . "<br/>";
      } catch (Exception $oops) {
        echo 'Caught exception: ',  $oops->getMessage(), "<br/>";
      }

4 Comments

Also close, but not correct. It is a filter, not an instantiation type. The code would perform different if that were the case. Take a look at my comment above...
@arkascha If you have the actual, accurate answer, please answer it so I can star you.
Yes, thanks, I already worked out that other substitutions would work, but could you point me to a reference for the syntax of the whole try catch block and the brackets following "catch" in particular.
Many thanks to both of you. I'm surprised I was unable to find this kind of explanation elsewhere on the web.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.