Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/11/2010, 15:40
Avatar de Xirant
Xirant
 
Fecha de Ingreso: noviembre-2010
Mensajes: 21
Antigüedad: 13 años, 6 meses
Puntos: 0
Excepción con excepción propia

Hola!

Bueno, ando haciendo un ejercicio de excepciones, tengo que dictaminar mi propia extensión de la Excepción principal y hacer en ella un switch. Pero no sé porqué (estoy segura de que es una tontería) no me coge la función getCode() correctamente. Seguro que me merezco un buen capón por ello :oS

Os pongo el código =>

Código PHP:
 <?php


        
// llamamos a la nueva excepcion que viene del padre Excepcion principal

        
class MiExcepcion extends Exception {
         

           public function  
__construct($message$code) {

               
// lo de arriba es el constructor de miExcepcion
               // y lo de abajo el constructor padre
                
parent::__construct($message$code);

            }

           function  
__toString() {

        switch ( 
getCode ()) { 


               case 
1:
                return 
"<table border='1' width='100%'>" "<tr>" 
                
"<td><strong>Excepcion " $this->getCode() . "</strong> : <br />" 
                
$this->getMessage() . " en la linea " $this->getLine() . " del fichero " 
                
$this->getFile() . 
                
"</td></tr></table>"  
                      
;
               

               case 
2:
                   return 
"<table border='1' width='100%'>" "<tr>" .
                
"<td><strong>Excepcion " $this->getCode() . "</strong> : <br />" .
                
$this->getMessage() . " en la linea " $this->getLine() . " del fichero " .
                
$this->getFile() .
                
"</td></tr></table>"
                      
;
               

               default:
                   return 
"<table border='1' width='100%'>" "<tr>" .
                
"<td><strong>Excepcion " $this->getCode() . "</strong> : <br />" .
                
$this->getMessage() . " en la linea " $this->getLine() . " del fichero " .
                
$this->getFile() .
                
"</td></tr></table>"
                      
;
               }
            }
       

        }

$email "someoneexample.com";
$email2 "[email protected]";
$patron "/^[a-z0-9]+([\.]?[a-z0-9_-]+)*@[a-z0-9]+([\.-]+[a-z0-9]+)*\.[a-z]{2,}$/";

try

{

    try

    {

        if(!
ereg($patron$email))

        throw new 
Exception($email1);

        if(
strpos($email"example") !== FALSE)

        throw new 
Exception($email22);

        echo 
"He llegado al final";

    }

catch (
Exception $e)

{
    
// se relanza la excepción

    
throw new customException($email,$e->getCode());

    
}

}

 catch (
customException $e)

 {

     echo 
$e->getCode();

 }



?>