Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
Can someone explain my how this exceptions works?

I found an really interesting problem on the internet about Exceptions & Errors, but I can't get it.

class MyException extends Exception {
    public function __construct(string $message) {
        $this -> message = $message;
    }
}

class A {
    public function __construct() {
        throw new MyException("an error appeared");
    }
}

$err = null;
try {
    new A();
}
catch (MyException $err) {
    throw new Exception('another error appeared');
}
catch (Exception $err) {
    echo $err;
}

When I execute the code I receive

Fatal error: Uncaught Exception: another error appeared in C:\xampp

I don't understand if it's a problem about the code or this is how it works actually. Maybe you can help me. That fatal error is for an untreated exception? Thank you!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Yes, the fatal error is for an unhandled exception here:

$err = null;
try {
    new A();
}
catch (MyException $err) {
--->throw new Exception('another error appeared');
}
catch (Exception $err) {
    echo $err;
}

In case you are wondering what is leading to this, it is this line in your code snippet:

$err = null;
try {
--->    new A();
}
over 4 years ago · Santiago Trujillo Denunciar

0

The second catch block does not catch the exception thrown in the first catch block. It can only be used to catch a an additional type of exception thrown in the first try block. To catch your second exception you need to add a nested try catch:

try {
    new A();
}
catch (MyException $err) {
    try {
        throw new Exception('another error appeared');
    }
    catch (Exception $err) {
        echo $err;
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda