SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Exception Without Using Try Catch Block

    Hi There..

    Im totally new to php..so please excuse me if my question looks like an idiot

    Is there a possibility to use exception withoust using try catch block

    Let say i have Thing Class
    PHP Code:

    <?php

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

            if (!
    $required)
            {
                throw new 
    Exception('$required is missing');

            }

            
    $this->bar();
        }

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

        public function 
    bar()
        {
            echo 
    "blar";
        }
    }
    And this how i use it

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

    See i need to use that class in try catch block..
    Its look really weird..Many php source code i have look into dosent have any try catch block in it
    If i use that class without try catch block,i will got Fatal error

    Is there a way that i can use that Thing class without wrap it in try catch block

    Just like this
    PHP Code:
    $myvar = new Thing($required); 

  2. #2
    SitePoint Member JAY6390's Avatar
    Join Date
    Oct 2009
    Location
    UK
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. You can use
    PHP Code:
    $myvar = new Thing($required); 

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by slier81 View Post
    Is there a way that i can use that Thing class without wrap it in try catch block
    No. Well, not unless you either:

    modify the class to somehow properly handle its own problem
    or
    you can make sure the exception never gets thrown

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Surely this is just bad design:
    PHP Code:
       public function __construct($required)
            
    $this->foo();

            if (!
    $required)
            {
                throw new 
    Exception('$required is missing');
            } 
    You should either give $required a default value, and check for that:

    PHP Code:
       public function __construct($required NULL )
            
    $this->foo();

            if ( 
    $required === NULL )
            {
                throw new 
    Exception('$required is missing');
            } 
    or, do it as originally posted and face a Fatal error because argument 1 is not set?

    Does this not all depend on whether this part of application has to :

    - die
    - try and carry on

    ?

  5. #5
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ignore the design pls..
    ya i know its kinda bad design..but the mian question is..i see other ppl code that use exception in their library,
    but when they use that library they dont wrap it in try catch block..
    i just want to mimic what they doing
    how about use set_exception_handler() ?

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, if you want your program to terminate, set_exception_handler() could be used.
    PHP Code:
    function myExceptionHandler ($e) {
        echo 
    "a bad error occurred that the programmer did not anticipate. exiting...";
    }
    set_exception_handler("myExceptionHandler"); 

  7. #7
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there any way that allow program does not terminate when using this set_exception_handler() ?

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't want the program to terminate, you need to handle the exception before it gets to your exception handler. The exception is an error condition, and must be addressed.

    You're probably gonna battle exceptions until you do some reading up on how they work, and why they are used.

    You could do something like this, but I don't think this is really any better.
    PHP Code:
    function make_a_thing_quietly($required) {
        try {
            return new 
    Thing($required);
        } catch (
    Exception $e) {
            return 
    false;
        }

    -You still need to check if it was successful.
    -You still need to do something else if it fails.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Brisbane, Australia
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why use Exceptions if you don't intend to catch them within your application? Use trigger_error instead.


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
  •