Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!