Results 1 to 7 of 7

Thread: PHP Try Catch Question

  1. #1
    Join Date
    Apr 2010
    Posts
    84

    PHP Try Catch Question

    I have just started with the PHP programming language, so I am not having much idea about it. I have recently started with the try.....catch block in PHP. I have understood the logic (since I know other programming languages also), but I am not able to to it practically. So I am expecting some help from you guys, because you should be knowing about it. Any other information related to the topic would be very grateful.!!

  2. #2
    Join Date
    Mar 2008
    Posts
    258

    Re: PHP Try Catch Question

    PHP 5 has an exception model similar to that other programming languages. An exception can be thrown (throw) and caught ("catch") in PHP. The code must be surrounded by a try block to facilitate the entry of a potential exception. Each try must have at least one corresponding catch block. Several catch blocks can be used to catch different classes of exceptions. The normal execution (when no exception is thrown within the try block or when a catch matching the thrown exception is not present) continues after the last catch block defined in the sequence. Exceptions can be launched (or relaunched) within a catch block.

  3. #3
    Join Date
    Apr 2008
    Posts
    193

    Re: PHP Try Catch Question

    A try catch is meaningful only if the code runs launched its errors via exceptions. So ... if in your code, you hesitate (via a dedicated class for example) a simple error handling: we test the errors as possible, and if wrong, you log a message, a code, the function name or its class. When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be sent with a message indicating that the exception could be caught unless a manager is defined with the function set_exception_handler ().

  4. #4
    Join Date
    Oct 2008
    Posts
    167

    Re: PHP Try Catch Question

    The code works perfectly and provides displays. "Capturing the exception: Always run this error Hello World!" The error is still triggered as it is launched in the try block. In this small example, an exception is thrown if variable $ a is 0.
    PHP Code:
    <Php 
    function hello () 

    0

    if ($ 
    == 1

    echo 
    "a is true"
    return 
    true

    else 

    throw new 
    Exception ("a is false"); 



    try 

    hello (); 

    catch (
    Exception e

    echo 
    "caught exception:". $ E-> getMessage (); 


    ?> 

  5. #5
    Join Date
    Dec 2008
    Posts
    202

    Re: PHP Try Catch Question

    The exception mechanism has been introduced to PHP in version 5 in addition to its new object-oriented model. Just as in Java, C + +, Action Script 3 with Visual Basic or (to cite only these programming languages), exceptions can simplify, customize and organize management "errors" in a computer program. PHP since version 5 has an object model similar to that of Java. To complement this new engine, the development team has integrated native PHP class Exception. This very special class allows the developer to simplify the treatment of exceptional cases that may arise during the execution of a program that generates objects of type Exception.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: PHP Try Catch Question

    • The first block of code declares four data members (attributes) of the class protected visibility. Respectively, these attributes record the error message, code, source file in question and the line at which the error was generated in the program.
    • Then comes the declaration of the constructor that takes two optional parameters. The first is the error message and the second the error code.
    • The third part of the body of the class for the declaration of 6 accessor methods that can not be redefined by inheritance. This restriction is represented by the final keyword. These methods respectively retrieve the error message, the error code, the source file in question, the line of the error in the file, and call stack.
    • Finally, we find the declaration of the magic method __toString () which can then be refined through inheritance.

  7. #7
    Join Date
    May 2008
    Posts
    2,297

    Re: PHP Try Catch Question

    The creation of an exception is made by calling the constructor of the native class Exception. The code below illustrates this step :
    PHP Code:
    <Php 
      
    / / Create Exception object 
      
    = new Exception ('An error is produced'); 
      / / 
    Display error message 
      
    echo $ -> getMessage (); 
      
    ?> 
    Notice the simplicity. The first line create object of type Exception ($ th) and automatically assigns the error message in the constructor. The second line of code displays the error message recorded on the standard output.

Similar Threads

  1. Some excellent games you are not able to catch
    By rUChIRr in forum Video Games
    Replies: 5
    Last Post: 16-05-2011, 10:35 AM
  2. Unable to catch fish in pet society
    By The$Nakul in forum Video Games
    Replies: 6
    Last Post: 28-02-2011, 10:22 AM
  3. Canon HF200/Windows catch 22
    By shimani in forum Portable Devices
    Replies: 5
    Last Post: 30-07-2010, 11:57 PM
  4. How to catch an Exception in Java?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 04:50 AM
  5. Explain Try Catch Block in java
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 02:13 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,714,155,854.60794 seconds with 17 queries