Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:17:51+00:00 2026-05-23T18:17:51+00:00

I witnessed some strange behaviour regarding PHP’s exception handling in a recent project. Case

  • 0

I witnessed some strange behaviour regarding PHP’s exception handling in a recent project. Case goes as follows.

In my app, I use namespaces. All classes are in individual source code files. The code relevant to this particular case, is spread over 3 classes.

The “outermost” class is a dispatcher (or router), which wraps the dispatch call inside a try-catch block. The dispatched request, calls a method in a third class, which runs code (wrapped in a try-catch block), which causes an exception.

Because I had omitted a use Exception; statement in the class where the error happens, the thrown exception trickles all the way back to the outermost layer (the dispatcher), where it is caught – causing me to scratch my head why the catch around the code causing the error isn’t working.

To me this seems strange. Logically, PHP should in this situation (IMO) throw a Class not found exception/error, leading me to the actual error in my code, instead of trying to “stay alive” as long as possible.

Should this be filed as a bug, or is this expected behaviour?

Edit: Code example

File: class-a.php

<?php 
namespace hello\world;

class classA {
    protected $b;

    public function __construct() {
        $this->b = new \hello\world\classB();
    }

    public function doSomething() {
        try {
            $this->b->throwException();
        } catch (Exception $e) {

        }
    }
}

File: class-b.php

<?php 
namespace hello\world;

class classB
{
    public function throwException() {
        throw new \Exception("bar closed");
    }
}

File: run.php

<?php 
include 'class-a.php';
include 'class-b.php';

$a = new \hello\world\classA();

$a->doSomething();

ClassB throws an \Exception in ClassB::doSomething(), for which ClassA has a catch-clause, but because ClassA doesn’t declare use Exception or catch (\Exception), the catch doesn’t match and execution ends with a Uncaught exception error1. But in my opinion, it should cause a Class not found error.

I might be expecting too much of the permissive PHP compiler, but it would help in tracking down silly errors that should be easy for the compiler to spot.

1 If the $a->doSomething() in run.php was surrounded by a try..catch clause, the Exception would (or at least could) be caught there, since it trickles down the stack.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:17:51+00:00Added an answer on May 23, 2026 at 6:17 pm

    PHP’s exception catching mechanism does not validate that the class you catch actually exists.

    It exhibits the same behavior when using typehinting in functions, so I suspect it merely converts the exception/function type hint into a string or something and compares that with the type of the relevant object.

    Whether this is a bug or not is questionable. Personally I think it should be classified as a bug, but PHP has all sorts of wonky behaviors 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just getting into CSS3 transitions (about time!) and I witness some strange behavior
I have now witnessed two companies move to agile development with Scrum. In both
This was a very bizarre scenario I just witnessed on a colleague's machine. While
I have witnessed how Internet Explorer 8 (IE8) is in Windows XP and how
Using Jess as the rule engine, we can assert a fact that some witness
My WinForms app uses a number of BackgroundWorker objects to retrieve information from a
I'm developing an app for a Facebook page with the 'timeline' layout, and I
No I'm really serious. I've just witnessed someone move variables local to a function
I have a project in which we would like to do the following :
Grandfathered Database, worst non normal form data I've ever witnessed. So queries become a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.