SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    PHP5: Return and Throw

    Hey all,

    Is this acceptable coding practice to return false and throw an exception at the same time? Like this
    PHP Code:
    public function Equals Object $var ) {
        try {
            if ( 
    $this->ToString() == $var->ToString() ) {
                return( 
    true );
            }
            return( 
    false );
        } catch ( 
    xException $ex ) {
            throw new 
    Exception 'className::Equals() threw and exception and was unable to complete' );
            return( 
    $ex->False );
        }

    So this kind of code will work
    PHP Code:
    if ( !$obj->Equals($oSomething) ) { 
    because the function returns false on error but also if it's enclosed in a try/catch, the error can be caught in another place and handled there.

    Is this good practise or not?

    Thanks

  2. #2
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will it even work? Coding style is personal preference -- All that matters in the end is the limitation of the interpreter.



    I think execution stops completely after you throw an error though, so I highly doubt that the interpreter will ever even get to your return.
    Ryan

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Well, with PHP5.0.0b1 this code
    PHP Code:
    <?php
    function foo $var ) {
        try {
            if ( !@include(
    $var) ) {
                throw new 
    Exception "Error" );
            }
        } catch ( 
    Exception $ex ) {
            throw 
    $ex;
            return 
    false;
        }
    }

    if ( !
    foo('nofile.php') ) {
        echo 
    'return false<br />';
    }

    try {
        
    foo('nofile.php');
    } catch ( 
    Exception $ex ) {
        
    $ex->GetMessage();
    }
    ?>
    displays this error in the browser
    Fatal error: Uncaught exception 'exception'! in Unknown on line 0
    Any ideas?

  4. #4
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, but it's never actually getting to the return, because it displays that every time there is an exception, and the only way to get to the return after the exception is thrown is to throw the exception, which stops the execution flow -- So, the return is never reached, right?
    Ryan

  5. #5
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As Gybbyl points out, there's no point in returning false as "normal" execution immediately stops the moment you "throw". In your example, the reason for the uncaught exception is here;

    PHP Code:
    if ( !foo('nofile.php') ) {
        echo 
    'return false<br />';

    That's not tested in a try / catch but causes the exception to be thrown with the foo function. This is working code;

    PHP Code:
    <?php
    function foo $var ) {
        try {
            if ( !@include(
    $var) ) {
                throw new 
    Exception "Error" );
            }
        } catch ( 
    Exception $ex ) {
            throw 
    $ex;
        }
    }

    try {
        
    foo('nofile.php');
    } catch ( 
    Exception $ex ) {
        
    $ex->GetMessage();
    }
    ?>
    Of couse the way you "re-throw" the exception in the foo() function is unnecessary in this example.

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Forgive me if I seem stupid. I still don't completely understand how
    exceptions should be used.
    But couldn't you just do this?

    PHP Code:
    <?php 
    function foo $var ) { 

        if ( !@include(
    $var) ) { 
            throw new 
    Exception "Error" ); 
        } 


    try { 
        
    foo('nofile.php'); 
    } catch ( 
    Exception $ex ) { 
        
    $ex->GetMessage(); 

    ?>
    If you require the exception to be caught on the outside of the function
    is there any reason to catch it inside the function?

  7. #7
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But couldn't you just do this?
    Yep - think DJ was experimenting with the possibilities though - not meant to be a real use example.

  8. #8
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Groningen, Netherlands
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  9. #9
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    OK, I think I've got this [i]mostly[/]i under control

    Thanks for the help!


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
  •