Exception handling in oop PHP not working

2019-08-03 07:56发布

问题:

I am very new to object oriented PHP and trying some basic examples to get good hand on oop php. I have simple exmaple above in which i am trying to learn exception handling and generate an exception error message when age is greater than 20 but not working.

<?php
interface read_methods 
{
    public function read_age($age);
}
abstract class  person 
{ 
    var $gender;
    var $animal;
    var $birds;
    abstract function group($group);
    function people($value)
    {
        $this->gender=$value;
    }
    function animals($value)
    {
        $this->animal=$value;
    }
    function bird($value)
    {
        $this->birds=$value;
    }
}

class behaviour extends person implements read_methods
{
    var $nonhuman;
    function get_all()
    {
        return $this->people();
        return $this->animals();
        return $this->bird();
    }
    function non_human($nonhuman)
    {
        return $this->alien=$nonhuman;
    }
    function read_age($age)
    {       
        try {
            $this->age=$age;
        }
        catch(Exception $e)
        {
            if ($age > 20)
            {
                throw new Exeption("age exceeds",$age, $e->getMessage());
            }
        }               
    }
    function group($group)
    {
        return $this->group=$group;
    }
}

$doerte=new behaviour();  
$doerte ->people(array('male','female'));
$doerte ->animals(array('fish','whale'));
$doerte ->bird(array('parrot','crow'));
$doerte->non_human('alien');
$doerte->read_age('23');
$doerte->group('living_things');
//$doerte->__autoload();
print_r($doerte);
?>

回答1:

you throw the exception at the wrong place.

If something is invalid you need to throw. That belongs to the try part. The catch part is used to handle the exception.

Think of it that way: you trow an exception if you programm has to yell out for help since it doesnt know what to do with data (try). The exception block should be used to take care of that cry for help

function read_age($age)
{       
    try {
        if($age > 20) {
            throw new Exception('Too old!');
        }
        $this->age=$age;
    }
    catch(Exception $e)
    {
        echo 'There has been an error: '.$e->getMessage();
    }               
}


回答2:

Do you want to create an exception when age exceeds 20? Here could be a solution (partial code):

/* ... */
function read_age($age)
    {       
        if ($age > 20) {
            throw new Exception("age exceeds, shoulw be less than 20");
        } else {
            $this->age=$age;
        }
    }
/* ... */

try {
    $doerte=new behaviour();  
    $doerte ->people(array('male','female'));
    $doerte ->animals(array('fish','whale'));
    $doerte ->bird(array('parrot','crow'));
    $doerte->non_human('alien');
    $doerte->read_age('23');
    $doerte->group('living_things');
    echo "It's all right";
    print_r($doerte);
} catch (Exception $e) {
    echo "Something went wrong: ".$e->getMessage();
}


回答3:

Have you checked your PHP error logs? Might be the misspelled "Exeption" which should be "Exception" => new \Exception(...

@Debflav: sorry for the double post. your answere hasn't shown up on my screen at the time