Results 1 to 4 of 4

Thread: Help: PHP exceptions handling explanation needed

  1. #1
    Join Date
    May 2008
    Posts
    13

    Help: PHP exceptions handling explanation needed

    Hi,

    Help: PHP exceptions handling explanation needed
    Can someone please explain me the basics & importance of exception handling in PHP? say PHP 5?
    I have not learned PHP in details but know some basics only. My friend told me to use exception handling but i don't know how to. I suppose this exception handling is a part of Java language.

    Thanks in advance.

    Regards,
    strangist

  2. #2
    Join Date
    May 2008
    Posts
    41

    Re: Help: PHP exceptions handling explanation needed

    What is an Exception

    PHP 5: Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.

    This is what normally happens when an exception is triggered:
    • The current code state is saved
    • The code execution will switch to a predefined (custom) exception handler function
    • Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code


    Different error handling methods:
    • Basic use of Exceptions
    • Creating a custom exception handler
    • Multiple exceptions
    • Re-throwing an exception
    • Setting a top level exception handler


    Basic Use of Exceptions

    When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block.
    If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.

    Try, throw and catch

    Proper exception code should include:

    1. Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
    2. Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
    3. Catch - A "catch" block retrieves an exception and creates an object containing the exception information

    Lets try to trigger an exception with valid code:

    PHP Code:
    <?php
    //create function with an exception
    function checkNum($number)
      {
      if(
    $number>1)
        {
        throw new 
    Exception("Value must be 1 or below");
        }
      return 
    true;
      }

    //trigger exception in a "try" block
    try
      {
      
    checkNum(2);
      
    //If the exception is thrown, this text will not be shown
      
    echo 'If you see this, the number is 1 or below';
      }

    //catch exception
    catch(Exception $e)
      {
      echo 
    'Message: ' .$e->getMessage();
      }
    ?>
    The code above will get an error like this:

    Code:
    Message: Value must be 1 or below

  3. #3
    Join Date
    May 2008
    Posts
    24

    Re: Help: PHP exceptions handling explanation needed

    Error Handling and Logging

    This is what you must go through.
    Here exception handling is explained in details & with examples.

    Exception handling is a must. Either your code fails, or external libraries you’ve grown to accept produce faults outside of your control.

    This is an interesting Article on Exception handling

    I hope this helps you.

  4. #4
    Join Date
    May 2008
    Posts
    13

    Re: Help: PHP exceptions handling explanation needed

    Thanks a lot guys for the explanation & the prompt reply.

    I hope these articles on error handling point me in right direction & ill be posting my problems in this section if I have problem understanding any concept.

    Thanks again.

Similar Threads

  1. Exceptions of PHP in set_exception_handler?
    By - Empty Shell - in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 02:12 AM
  2. What are Exceptions in PHP 5?
    By DANIEL 602 in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 01:35 AM
  3. What is Log .NET Exceptions
    By Agneya in forum Software Development
    Replies: 4
    Last Post: 07-02-2010, 01:34 AM
  4. PHP Error exceptions
    By nehal_serpa in forum Software Development
    Replies: 3
    Last Post: 01-05-2009, 06:55 PM
  5. Replies: 5
    Last Post: 17-07-2008, 02:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,712,531.72269 seconds with 17 queries