sábado, 20 de diciembre de 2025

try catch finally

<?php


function rankUser($age) {

    $rank = null;

    $status = null;


    try {

        if ($age >= 18 && $age <= 65) {

            $status = "allowed";


            if ($age < 45) {

                $rank = "Rookie";

            } else {

                $rank = "Veteran";

            }


        } else {

            $status = "denied";

            throw new Exception("Age must be between 18 and 65\n");

        }


    } catch (Exception $e) {

        echo 'Caught exception: ' . $e->getMessage() . "\n";

    } finally {

        if ($rank) {

            echo "User status $status, and Rank $rank";

        } else {

            echo "Sorry, your status is $status. You don't meet age requirements.";

        }

    }

}


rankUser(13);


?>


No hay comentarios:

Publicar un comentario