Ejemplo utilizando try catch throw finally en php

En este ejemplo mostrare un ejemplo utilizando la siguientes funciones: try catch throw finally en php dentro de una misma validación de datos en php.


aqui el codigo:


<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8">
    <title>try catch throw finally</title>
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<section>

<?php
//utilizando throw con uan funcion
function verificar_num5($num){
    if($num==5){
        //crera tipo de error quien no queramos que ocurra
        throw new exception('Cuidao, utilizas the number cinco');
    }
}
    //try verifica funcion
    try{
        verificar_num5(5);
        //catch utiliza metodos internos encaso de error
    }catch(exception $e){
echo "Excepcion capturada: {$e->getMessage()}<br>";
     
echo "El tipo de codido es: {$e->getCode()}<br>";
     
echo "La ruta del arhcivo que falla es {$e->getFile()}<br>";      
        //utilizando finally
    }finally{
        echo "Exito<br>";
    }

?>
 
</section>
</body>
</html>



Ejemplo de try en php.




PHP

Comentarios

Entradas más populares de este blog

Solucion al error collation desconocida: 'utf8mb4_0900_ai_ci'

Ejemplo de suma Pascal