Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda