CKTIS / PHP
Exception
Describe una condición excepcional con mensaje, código entero y causa Throwable opcional. throw interrumpe el flujo hasta un catch coincidente.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
new Exception($message, $code, $previous)Describe una condición excepcional con mensaje, código entero y causa Throwable opcional. throw interrumpe el flujo hasta un catch coincidente. Al traducir el error pasa el original como previous. Exception por sí sola no incluye Error. El método Exception::getMessage devuelve el texto de esa excepción, aquí «job». El mensaje de la causa «source» se obtiene por separado con $e->getPrevious()?->getMessage() si existe una excepción anterior.
$cause=new RuntimeException("source",8);
try { throw new Exception("job",12,$cause); }
catch(Exception $e){echo json_encode([$e->getMessage(),$e->getCode(),$e->getPrevious()->getMessage()]);}
["job",12,"source"]
Ejemplos
<?php
$cause=new RuntimeException("source",8);
try { throw new Exception("job",12,$cause); }
catch(Exception $e){echo json_encode([$e->getMessage(),$e->getCode(),$e->getPrevious()->getMessage()]);}
Resultado
["job",12,"source"]