Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How long is exceptions thrown?
Old 01-30-2009, 04:20 PM How long is exceptions thrown?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Here's a thing I've been wondering about for a while. Anybody know?

Scenario:

In the constructor in class A a RandomException can be thrown. There is also a class B which extends A, and in it's constructor it simply calls A's constructor with parrent::A();

When using the class B, I catch the exception, like so
PHP Code:
try {
   
$var = new B();
} catch(
RandomException $e) {
   echo 
"Fail!";

Now the question. Is the exeption in A thrown tru B and catched above, or do I need to catch and re-throw it in B, like this?

PHP Code:
class {
   function 
B() {
      try {
         
parent::A();
      } catch(
RandomException $e) {
         throw 
$e;
      }
   }

lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
 
Register now for full access!
Old 01-30-2009, 05:27 PM Re: How long is exceptions thrown?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Before I try answer your question I just want to point out that you are using PHP4 syntax for your custructor. In PHP5 the constructor function for any class is called __construct()

Now to answer your question, the parent constructor of a class is not called implicity if your child class has it own constructor definition. So if B has its own constructor, the constructor of A won't be called unless you do so explicitly (parent::__construct or parent::A() for PHP4). So I think you would have to catch and re-throw as in your second code block.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 01-30-2009, 08:37 PM Re: How long is exceptions thrown?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Oh, thank you for the enlightenment I actually thought it was the other way around.

But what if do like this?

PHP Code:
class {
   function 
__construct() {
      
// some code
      
throw new Exception("oops!");
   }
}

class 
extends {
   function 
__construct() {
      
parent::__construct();
   }
}

try {
   
$b = new B();
} catch(
Exception $e) {
   echo 
"fail!";

Now A's construct is called from B's, and throws and exception. Will B generate an fatal error "Uncaught Exception" or will it go pass B and be caught in the catch below?
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 01-31-2009, 06:35 PM Re: How long is exceptions thrown?
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
You should try it on xampp ( http://www.apachefriends.org/en/xampp.html ), it lets you run php scripts on your local machine.
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 01-31-2009, 07:05 PM Re: How long is exceptions thrown?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You may have to modify B's constructor like so:
PHP Code:
class extends A
{
     public function 
__construct()
     {
          try
          {
               
parent::__construct();
          }
          catch(
Exception $e)
          {
               throw 
$e;
          }
     }

Don't take my word for it however, you should run it both ways and figure out which works.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 02-01-2009, 05:50 AM Re: How long is exceptions thrown?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I'm already running WAMP at my computer. But I wasn't at home at the time I posted and I really wanted to know
However, I've tested it now. It seems the try-catch in B is totaly unnecessary.

PHP Code:
class {
    function 
__construct() {
        echo 
"This is A<br />";
        throw new 
Exception("oops!");
    }
}

class 
extends {
    function 
__construct() {
        echo 
"This is B<br />";
        try {
            
parent::__construct();
        } catch(
Exceptin $e) {
            throw new 
Exception("oh no!");
        }
    }
}

try {
    echo 
"Creating B...<br />";
    
$b = new B();
} catch(
Exception $e) {
    echo 
$e->getMessage();

This code produces:

Creating B...
This is B
This is A
oops!

The exception is never caught in B, and if I remove the try-catch surounding '$b = new B();' I get a fatal error 'Uncaught exception'. The try-catch in B however, can be removed without it making any difference. Guess I have some editing to do now

Thanks
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-01-2009, 03:09 PM Re: How long is exceptions thrown?
Average Talker

Posts: 15
Trades: 0
Thought i would clarify exceptions for you. When you throw an exception if not caught in it will continually move up the stack until it reaches top level. Obviously at top level if not caught if will throw a fatal error and terminate the script.


The error should be caught in object B in your above code. If that is the exact test code the reason it was not caught is because you spelt exception wrong.


Regards, George
GOPalmer is offline
Reply With Quote
View Public Profile
 
Old 02-01-2009, 03:20 PM Re: How long is exceptions thrown?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Ah, I see. Thank you.
Indeed it was caught in B. Thanks for noticing my spelling error
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-01-2009, 03:58 PM Re: How long is exceptions thrown?
Average Talker

Posts: 15
Trades: 0
No problem.

Regards, George
GOPalmer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How long is exceptions thrown?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31159 seconds with 12 queries