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

  • SEARCH
  • Home
  • 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 1031067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:52:13+00:00 2026-05-16T13:52:13+00:00

We all agree that using different exception types for different tasks is the way

  • 0

We all agree that using different exception types for different tasks is the way to go.

But then, we end up with creating ghost files like this:

/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Dojo
 * @subpackage View
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $
 */

/**
 * @see Zend_Dojo_Exception
 */
require_once 'Zend/Dojo/Exception.php';

/**
 * @category   Zend
 * @package    Zend_Dojo
 * @subpackage View
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Dojo_View_Exception extends Zend_Dojo_Exception
{
}

Then the same for Zend_Dojo_Exception and the same for Zend_Exception…

Is there any generic approach for this problem?

Something like throw new \My\Just\Declared\Exception\ (which extends \My\Just\Exception), so I didn’t have to crate and require all those ghost files?

  • 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-16T13:52:14+00:00Added an answer on May 16, 2026 at 1:52 pm

    In good practice, not really… There are however some hacks you could do if you REALLY wanted to do this, but I still think they are more evil.

    For example, one of those hacks is to eval those classes into existance via an autoloader. This is bad because if someone ever greps for the exception’s definition or for the exceptions that your package throws they are going to be a whole lot of nada in return…

    public static function load($name) {
        $parts = explode('_', $name);
        if (strtolower(end($parts)) == 'exception') {
            //make it extend the proper exception
            array_pop($parts); //get rid of the last Exception bit
            array_pop($parts);
            $parts[] = 'Exception';
            $parent = implode('_', $parts);
            $code = 'class '.$name.' extends '.$parent . '{}';
            eval($code);
        }
    }
    

    But again, let me stress this is usually a bad idea.

    Personally, I inherit from multiple “base exceptions” (typically the SPL exceptions). So for example a Database_Connection_Exception might extend RuntimeException, trying to commit a non-open transaction might throw a Database_Not_In_Transaction_Exception which might extend LogicException. The point being that declaring them separately lets you do more than just straight heiarchal inheritance (not to mention is better for documentation, since people can look at a glance at the defined exceptions, and you can actually override methods to better suit your needs if appropriate)…

    Edit: Based upon your mention of Zend‘s tendency to do one exception per sub-package, here’s how I do it…

    Basically, I have a few “global” exceptions that are used throughout the application(s). These include (but are not limited to): ClassNotFoundException, FileNotFoundException, NotCallableException and a bunch of others. Basically just those that aren’t package specific, but need to convey more meaning than the core PHP exceptions can…

    Then, I declare exceptions on a package level only. In that directory (package/exceptions) I declare each and every exception as necessary. So one subpackage may have 5 or 10 exceptions to distinguish different conditions, while another subpackage (within the same package) may have none. So I declare them as needed so that the exception means what happened.

    I do this for a simple reason. I don’t care about where the exception was thrown from (And if I really did, I can inspect the backtrace that’s automatically generated inside of the exception). I care about why the exception was thrown. And that lets me properly handle the exception…

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

Sidebar

Related Questions

I think we can all agree that Automatic Properties in C# 3.0 are great.
First of all I agree that mocking external API calls is the right thing
All I want to do is return the index of the i that is
all. We're trying to get some intersect collisions working, but the problem experience is
All my files can be found on GitHub: https://github.com/Integralist/Passage (just in case you need
I have a class that handles all the interaction in my application with my
This question has been done to death, and I would agree that enums are
I am using an onsubmit variable to ensure that the user really means to
I'm using two different libraries in my project, and both of them supply a
I'm using xmlseclibs to try and sign a SOAP document, but it does not

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.