try manejo manejador exceptions excepciones errores custom catch all php exception-handling

manejo - try catch php laravel



php gestiĆ³n personalizada de excepciones (4)

Estoy queriendo manejar excepciones en mi aplicación php yo mismo.

Cuando lanzo una excepción, quiero pasar un título para usar en la página de error.

¿Puede alguien vincularme a un buen tutorial, o escribir una explicación clara de cómo funciona realmente la excepción (por ejemplo, cómo saber qué tipo de excepción está tratando con ect.


Los documentos oficiales son un buen lugar para comenzar: http://php.net/manual/en/language.exceptions.php .

Si solo se trata de un mensaje que desea capturar, lo haría de la siguiente manera;

try{ throw new Exception("This is your error message"); }catch(Exception $e){ print $e->getMessage(); }

Si quiere capturar errores específicos, debería usar:

try{ throw new SQLException("SQL error message"); }catch(SQLException $e){ print "SQL Error: ".$e->getMessage(); }catch(Exception $e){ print "Error: ".$e->getMessage(); }

Para el registro, necesitaría definir SQLException . Esto se puede hacer tan simple como:

class SQLException extends Exception{ }

Para un título y mensaje, extendería la clase Exception :

class CustomException extends Exception{ protected $title; public function __construct($title, $message, $code = 0, Exception $previous = null) { $this->title = $title; parent::__construct($message, $code, $previous); } public function getTitle(){ return $this->title; } }

Puede invocar esto usando:

try{ throw new CustomException("My Title", "My error message"); }catch(CustomException $e){ print $e->getTitle()."<br />".$e->getMessage(); }


Primero, recomendaría echar un vistazo a la página correspondiente del manual de PHP , es un excelente lugar para comenzar. Además, puede consultar la página Extendiendo excepciones - hay más información sobre la clase de excepción estándar y un ejemplo de implementación de excepción personalizada.

Si la pregunta es cómo hacer alguna acción específica si se lanzó una excepción de tipo particular, entonces solo tiene que especificar el tipo de excepción en la declaración catch:

try { //do some actions, which may throw exception } catch (MyException $e) { // Specific exception - do something with it // (access specific fields, if necessary) } catch (Exception $e) { // General exception - log exception details // and show user some general error message }


Pruebe esto como lo primero en su (s) página (s) php.

Captura errores y excepciones de php.

function php_error($input, $msg = '''', $file = '''', $line = '''', $context = '''') { if (error_reporting() == 0) return; if (is_object($input)) { echo "<strong>PHP EXCEPTION: </strong>"; h_print($input); $title = ''PHP Exception''; $error = ''Exception''; $code = null; } else { if ($input == E_STRICT) return; if ($input != E_ERROR) return; $title = ''PHP Error''; $error = $msg.'' in <strong>''.$file.''</strong> on <strong>line ''.$line.''</strong>.''; $code = null; } debug($title, $error, $code); } set_error_handler(''php_error''); set_exception_handler(''php_error'');