Jump to content

Need help using try/catch exception handeling


eldan88
Go to solution Solved by gristoi,

Recommended Posts

Hey Guys. I am across this in a book and I am having a hard time understanding how I can try/catch this expception

 

The code below will throw an exception if I add an unit object into Archer or LaserCannon object.

 

Here is a quick example

class UnitException extends  Exception{}

abstract class Unit{



    function AddUnit(Unit $unit){
        throw new UnitException(get_class($this)."is a leaf");
    }
    function removeUnit(Unit $unit){
        throw new UnitException(get_class($this)."is a leaf");

    }

    

    abstract function bombardStrength();


}

class Archer extends  Unit {
    function bombardStrength(){
        return 4;
    }
}




class LaserCannonUnit extends  Unit {
    function bombardStrength(){
        return 44;
    }
}
$laser_cannon = new LaserCannonUnit();
$laser_cannon->AddUnit(New Archer()); // This is not allowed

When I run the client code I get the following error message

 

Fatal error: Uncaught exception 'UnitException' with message 'LaserCannonUnitis a leaf'

 

Can any please help me understand this?? Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.