9

For the PHP pros: Is there a way of turning notices and warnings into exceptions thrown in the scope of occurence without hacking the interpreter?

The answer most likely is "No!" - but if there is another way i certainly would like to know it...

Comments
  • 1
    Outside of rolling your own error handler and throwing exceptions that way 🤔

    PHP7 has a lot of throwable errors you can try/catch with but as for warnings, can't say I've set them up as errors / exceptions before. Not all warnings are erroneous.
  • 0
    @C0D4
    I already have my own global error handler.
    But it would be nice to be able to "catch" notices and warnings in the calling code.
  • 3
  • 4
    @Oktokolo off the top of my head, using Trigger_error() with E_Warning / E_Notice and having your own error handler to output debug_backtrace() so you have some useful output could work.
  • 0
    @C0D4
    Thanks for the nudge.
    Out of curiosity i tried throwing an exception inside the error handler and it indeed just worked. Even the trace is correct.

    Can't believe i didn't try that back then when i wrote the global error handler...
  • 1
    @vintprox this could work too.
  • 2
    Just a reminder to be careful...

    If you have an exception handler - it must not generate an exception or error.

    Relevant as you can trigger a fun recursion when the error handle generates an exception, the exception handler kicks in, throws an error, the error handler kicks in.... You get the idea.
  • 1
    Yes, don't use PHP. All warnings will go away, instantly.
Add Comment