Exceptions, the code doesn't work


#1

Hello,

I am going through the ebook “The Essentials of the Object Oriented PHP: Learn, Practice, and Apply”, it’s a great book btw. I got stuck at the final coding exercise for exceptions, I can’t see any error here why this one is not working, any clues, help? Thanks
`<?php

Class User {
private $name;
private $age;

public function setName($name)
{
    $name = trim($name);

    if(strlen($name) < 3)
    {
        throw new Exception("The name should be at least 3 characters long.");
    }

    $this->name = $name;
}

public function setAge($age)
{
    $age = int($age);

    if($age < 1)
    {
        throw new Exception("The age cannot be zero or less.");
    }

    $this->age = $age;
}

public function getName()
{
    return $this->name;
}

public function getAge()
{
    return $this->age;
}

}

$dataForUsers = array (
array(“Ben”,4),
array(“Eva”,28),
array(“li”,29),
array(“Catie”,“not yet born”),
array(“Sue”,1.5)
);

foreach($dataForUsers as $data => $value)
{
try
{
$user = new User();

    $user -> setName($value[0]);
    $user -> setAge($value[1]);

    echo $user -> getName() . " is " . $user -> getAge() . " years old. <br/>";
}

catch (Exception $e)
{
    echo "Error: " . $e -> getMessage() . " in the file: " . $e -> getFile() . " on line: " . $e -> getLine() . "<hr/>";
}

}`


#2

You need to tell or show us what exactly - ‘doesn’t work’ means, even if you are getting a blank page, that’s a clue that narrows down the possibilities.

However, there’s a problem that would be producing a fatal php run-time error. Do you have php’s error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your system, so that php will help you by reporting and displaying all the errors it detects? Stop and start your web server to get any changes made to the php.ini to take effect and use a phpinfo() statement in a .php script to confirm that the changes actually took affect (in case the php.ini that you changed is not the one that php is using.)


#3

You’re right, I forgot to write that. It displays this, essentially error 500:

This page isn’t working

localhost is currently unable to handle this request.

HTTP ERROR 500

The console returns also:
Failed to load resource: the server responded with a status of 500 (Internal Server Error).

I’ve checked and the error reporting is set to E_ALL in the php.ini


#4

Hello, i’m a beginner and my opinion may not matter here. However, i notice two throws and no catch. I did this the other day and my database script stopped working. The reason is because one cannot throw and exception without catching it. The catch at the bottom works for the try, not the two throw statements. Comment out the throw statements and see if it works.

If the throw statements are the reason, then the author of the book…


#5

The OP’s usage of exceptions is correct. The methods where they are being thrown are being executed within the try block.

Also, if you don’t catch an exception in your code, php will catch it, where it will use its error_reporting, display_errors, and log_errors settings to control what happens with the actual error information (display it or log it.) If you didn’t see or log an error when you weren’t catching the exception, the php settings are probably not set correctly. In general, let php catch and handle fatal things and your code should catch and handle recoverable things.