Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: PHP - Exception Handling won't work!  (Read 4451 times)

0 Members and 1 Guest are viewing this topic.

ultimatum

    Topic Starter


    Intermediate
  • Thanked: 3
    PHP - Exception Handling won't work!
    « on: December 02, 2009, 05:13:05 PM »
    I'm trying to catch a general exception in PHP using try and catch blocks.

    This is my code:

    <?php

    $a = 5;
    $b = "word";

    try
    {
       $c = $a+$b;
    }

    catch (Exception $e)
    {
       echo "Message: addition failed."
    }

    echo $c;


    ?>

    I placed a string in $b variable on purpose to test the exception handling but when I run the application I get this error:

    Parse error: parse error, unexpected '{' in w:\home\localhost\www\...\phpdesigner_tmp6.php on line 7

    Line 7 being the try { code.

    Any idea why this happens? Is there any adjustments I have to make in php.ini for this to work? Any help is greatly appreciated.

    Thanks.
    Its not what you know, its what you can do that counts!

    Treval



      Hopeful

      Thanked: 14
      Re: PHP - Exception Handling won't work!
      « Reply #1 on: January 21, 2010, 11:53:43 AM »
      Looks like a very easy error to me.
      You can't calculate Strings with numbers.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: PHP - Exception Handling won't work!
      « Reply #2 on: January 21, 2010, 12:24:15 PM »
      Looks like a very easy error to me.
      You can't calculate Strings with numbers.

      That would have been a run-time error, and in fact, I don't think it would cause a problem.

      The issue is actually caused by a missing ; on the "addition failed" echo line.

      Code: [Select]
      <?php

      $a 
      5;
      $b "word";

      try
      {
         
      $c $a+$b;
      }
      catch (
      Exception $e)
      {
         echo 
      "Message: addition failed.";
      }

      echo 
      $c;


      ?>


      it doesn't trigger the exception, but it only shows 5; since + is only defined for numbers. You'd expect that to raise an exception, but it doesn't. In fact, it doesn't even cause an error.

      Even as it is now, the try...catch won't catch runtime exceptions, like division by zero; because these are (for some reason) handled by the interpreter itself; in fact, the only exceptions you can Catch with a Try block are the ones you Throw yourself.

      You can cause errors to be handled as exceptions if you define your own error handler routine:

      Code: [Select]
      function errorHandler($number, $string, $file = 'Unknown', $line = 0, $context = array())
      {
          if (($number == E_NOTICE) || ($number == E_STRICT))
              return false;

          if (!error_reporting())
              return false;

          throw new Exception($string, $number);

          return true;
      }

      set_error_handler('errorHandler');

      It still won't catch the issue with + and strings; although I suppose that is more the domain of a warning. It will catch things like division by zero and file not found errors, though.

      EDIT: is it just me, or is my first code tag syntax highlighted?
      I was trying to dereference Null Pointers before it was cool.

      kpac

      • Web moderator


      • Hacker

      • kpac®
      • Thanked: 184
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      Re: PHP - Exception Handling won't work!
      « Reply #3 on: January 21, 2010, 12:26:38 PM »
      Quote
      EDIT: is it just me, or is my first code tag syntax highlighted?
      Yep, you forgot your PHP opening and closing tags in the second.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: PHP - Exception Handling won't work!
      « Reply #4 on: January 21, 2010, 12:27:15 PM »
      Yep, you forgot your PHP opening and closing tags in the second.

      naw, I didn't forget them, since it is supposed to be inserted  within existing PHP :P
      I was trying to dereference Null Pointers before it was cool.

      ultimatum

        Topic Starter


        Intermediate
      • Thanked: 3
        Re: PHP - Exception Handling won't work!
        « Reply #5 on: January 21, 2010, 01:16:18 PM »
        Thank you for your replies. I ended up using error suppression in my php code. I've done some research and I noticed not many people use exception handling, for the most part it is just avoided using if and else statements.

        Do any of you use exception handling?
        Its not what you know, its what you can do that counts!

        Treval



          Hopeful

          Thanked: 14
          Re: PHP - Exception Handling won't work!
          « Reply #6 on: January 21, 2010, 06:02:06 PM »
          I used it in my java exam. lol.
          Custom error throwing and catching elsewhere. :)

          But that's java.

          ultimatum

            Topic Starter


            Intermediate
          • Thanked: 3
            Re: PHP - Exception Handling won't work!
            « Reply #7 on: January 21, 2010, 09:07:53 PM »
             :) I use it in Java as well.
            Its not what you know, its what you can do that counts!

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: PHP - Exception Handling won't work!
            « Reply #8 on: January 22, 2010, 08:53:15 AM »
            C#, VB.NET, and I used Win32 SEH (Structured Exception Handling) for a while in C++, but I haven't used C++ in quite some time. I sort of use Exceptions in VB6; one of my projects has a infrastructure that sort of emulates Exception handling. It obviously doesn't have it's own syntax; but it emulates it pretty well.

            I haven't really used Java for a long time (back when AWT was the way to create User Interfaces) it had exception handling then, but I don't think I actually used it (most of my projects were pretty trivial). My favourite feature in the newer languages that have developed has got to be generics. They save a ton of typing.

            With regards to PHP, I think that Exceptions are more useful when your using Classes and have an object heirarchy; most PHP pages don't really need their own set of objects, and usually errors can be handled in-line, so it's really not something you see a whole lot of.
            I was trying to dereference Null Pointers before it was cool.

            Treval



              Hopeful

              Thanked: 14
              Re: PHP - Exception Handling won't work!
              « Reply #9 on: January 26, 2010, 10:18:05 PM »
              Now there's Swing in java. :D