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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:37:59+00:00 2026-05-16T05:37:59+00:00

I came across some code recently that used a custom error handler to turn

  • 0

I came across some code recently that used a custom error handler to turn any PHP errors into an generalized application exception. A custom exception handler was also defined that would log the exception if it was within a particular error code range. Example:

class AppException extends Exception
{

}

function error_handler($errno, $errstr, $errfile, $errline)
{
    throw new AppException($errstr, $errno);
}

function exception_handler($exception)
{
    $min = ...;
    $max = ...;

    if ($exception->getCode() >= $min && $exception->getCode() <= $max)
    {
        // log exception
    }
}

set_error_handler('error_handler');
set_exception_handler('exception_handler');

$a[1]; // throws exception

The problem is that I saw things like:

try
{
    do_something();
}
catch (AppException $exception)
{
}

Which implies that there is no distinction between actual programming errors and “Exceptional” behavior. Upon further digging, I came across parts of the code that were designed around the idea that PHP errors represented “Exceptional” behavior such as:

...

function my_function($param1, $param2)
{
    // do something great
}

try
{
    my_function('only_one_param');
}
catch (AppException $exception)
{
}

Which ends up obfuscating errors and the design of the application’s interface.

What is your opinion on handling errors this way? Is it worth turning PHP’s native errors into exceptions? What do you do in situations like the above where a codebase is designed around this idea?

  • 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-16T05:38:00+00:00Added an answer on May 16, 2026 at 5:38 am

    Personally, I do this all the time. The only difference is that in my error_handler function, I check to see if the error is an E_NOTICE first, and only throw if it is not (I log the notice anyway)…

    I would change the AppException to something that extends ErrorException… Something like: PhpRuntimeErrorException extends ErrorException which you ONLY use for PHP errors… The reason is so that it’s more readable (it’s easier to tell what a PhpRuntimeErrorException is without needing to figure out where it’s thrown). The other reason, is that the ErrorException will store the generating line/file/etc information, where it would not be stored elsewhere (since the backtrace starts from the throw line)…

    So, then you can “try” code like this:

    try {
        $f = fopen('foo.bar', 'r');
        $ret = '';
        while ($data = fread($f)) {
            $ret .= process($data);
        }
        fclose($f);
        return '';
    } catch (PHPRuntimeErrorException $e) {
        throw new RuntimeException('Could not open file');
    } catch (ProcessException $e) {
        fclose($f);
        throw new RuntimeException('Could not process data');
    }
    return $ret;
    

    I also make my default exception handler generate a 500 server error page. That’s because any exceptions should be caught, and if they were not, it really is a server error…

    Just my experience and opinion…

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

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd recommend two resources: Herb Sutter's Effective Concurrency articles Anthony… May 16, 2026 at 3:56 pm
  • Editorial Team
    Editorial Team added an answer Change: $(this).focusNextInputField(); //Here is the problem Into: $(event.target).focusNextInputField(); //No longer… May 16, 2026 at 3:56 pm
  • Editorial Team
    Editorial Team added an answer check out this bookmarklet called RefreshCSS by Paul Irish: javascript:(function(){var… May 16, 2026 at 3:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I recently came across some Java code that simply put some strings into a
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...
I was working on some code recently and came across a method that had
An interesting issue came up recently. We came across some code that is using
We recently came across some sample code from a vendor for hashing a secret
I recently came across classes that use a configuration object instead of the usual
I recently came across code written by a fellow programmer in which he had
I am currently reading some C++ source code, and I came across this: double
I was going through someone's code where I came across a thread: while(TRUE) {
Recently I've come across a problem in my project. I normally compile it in

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.