SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict blizzman24's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is this proper use of exceptions?

    PHP Code:
    //in class
    if(!require 'file.php'){
       throw new 
    Exception("file.php is needed");


    Does that above statement work? It's located in a class object. Does it include file.php in the if statement?

    PHP Code:
    try{
    myvar = new Thing();
    }
    catch(
    Exception $e){
      echo(
    $e-> getMessage());
    exit()


  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it's proper exception use. But the code won't work. If require fails, a fatal error occurs immediately. You could use include.

    But, beware of the return value of include/require. If the file has a return statement in the files global scope, that will be used as the return value. See the documentation for include for more info.

    Also be aware that if the code inside that file throws an exception, you will print an error message saying file is needed, even though the file was included. You could use a more specific type of exception if you want to distinguish.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the code inside the file throws an exception, it won't say that file.php is needed, because the exception is thrown before it gets to the 'throw new Exception("file.php is needed");' line.

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 issued with an "Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler()

    test:
    PHP Code:
    <?php

    class Thing
    {
        public function 
    __construct()
        {
            
    $this->foo();

            if (! require 
    'file1.php')
            {
                throw new 
    Exception("file.php is needed");

            }

            
    $this->bar();
        }

        public function 
    foo()
        {
            echo 
    "bla";
        }

        public function 
    bar()
        {
            echo 
    "blar";
        }
    }

    try
    {
        
    $myvar = new Thing();
    }
    catch (
    Exception $e)
    {
        
    $e->getMessage();
    }
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    If the code inside the file throws an exception, it won't say that file.php is needed, because the exception is thrown before it gets to the 'throw new Exception("file.php is needed");' line.

    oh, ya. good "catch". heh


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
  •