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 7652453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:46:25+00:00 2026-05-31T11:46:25+00:00

I have to develop an exception handler that should handle like 5 different type

  • 0

I have to develop an exception handler that should handle like 5 different type of exceptions. Let’s call them simply Ex1, Ex2, Ex3…

I though of doing a single class called ExHandler which will be instantiated like this:

...
} catch (Ex1 $e) { $h = new ExHandler($e); $h->render(); }
catch (Ex2 $e) { $h = new ExHandler($e); $h->render(); }
catch (Ex3 $e) { $h = new ExHandler($e); $h->render(); }
...

And inside ExHandler manage each different Exception differently using $e instance of Ex1, $e instance of Ex2, $e instance of Ex3…

But It doesn’t seems a very good practice to me. Is it good? Is there any other way of doing this?
Should I create an Ex1Handler, Ex2Handler, Ex3Handler…? My S.O.L.I.D spirit tells me something is just wrong here. What is it?

  • 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-31T11:46:26+00:00Added an answer on May 31, 2026 at 11:46 am

    I need to note before I answer this, that procedural programmers will look at this and think it’s dumb 🙂 but I can live with that, this is assuming an OOP application with HTML templating that outputs after the output_buffer is cleaned.

    I always create a try/catch block encompassing the majority of my code in one call usually at the point where I start requiring other files as well as starting an output_buffer whilst in development.

    ob_start();
    
    try {
        switch($appPage) {
            case('work'):
                require_once('im_bored_at_work.php');
                break;
            case('home'):
                require_once('im_a_little_less_bored_at_home.php');
                break;
            default:
                require_once('on_the_fence.php');
        }
    } catch (Exception $e) {
        // Handle exception caught and apply formatting
    }
    
    $devOut = ob_get_contents();
    ob_end_flush();
    

    To give an example how I would handle the multiple exceptions you need to catch with a custom class

    class CustomExceptionHandler extends Exception {
    
        private $msg;
        private $code;
        private $otherVars;
    
        public function __construct($msg,$code=0,$otherDebugVar=null){
            $this->msg = $msg != null ? $msg : "An unknown exception was thrown";
            $this->code = $code;
            $this->otherVars = $otherDebugVar;
            parent::__construct($msg,$code);
        }
    
        public function getOtherVars() {
            return $this->otherVars;
        }
    
    }
    

    The idea is to just keep the custom information within the exception object, and when you rethrow the exception at the end of a try/catch block as a standard exception you include the formatted custom message, it shouldn’t really matter now which Exception handler picked up the original exception as all the info you will need will come downstream and be caught in the original try / catch block.

    class BasicTemplate {
        private $template;
        private $path;
        private $contents;
    
        public function __construct($template, $path) {
    
            $this->template = $template;
            $this->path = $path;
            $this->buildTemplate();
        }
    
        private function buildTemplate() {
    
            if ($contents = @file_get_contents($this->path . $this->template)) {
                $this->contents = $contents;
            } else {
                $e = new CustomExceptionHandler("Message",2,$this->path . $this->template);
                // Do whatever else you want to do with custom exception handling class
                throw $e;
            }
        }
    }
    

    Now you need to catch your exception and rethrow it:

    try {
        $html = new BasicTemplate($temp,$path);
    } catch {CustomExceptionHandler $e) {
        throw new Exception("Message: {$e->getMessage()} Other Info: {$e->getOtherVars()}",$e->getCode());
    }
    

    That’s the rough idea anyhow, hope it helps.

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

Sidebar

Related Questions

I have to develop an application using C#.net that has to be run once
I have to develop software for a USB scale that, when you press a
I have to develop a tool in C# that retrieves some data from an
I have been assigned a project to develop a set of classes that act
I have develop small Asp.net MVC3 application using Telerik rad Controls with in that
I have develop an XNA game on computer 1. When I send it to
I have develop one web site in php. I know how to upload it
Hi i have develop and deploy one multilingual (english uk and swedish) website at
I have to develop an application which parses a log file and sends specific
I have to develop a database for a unique environment. I don't have experience

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.