When to use Exceptions vs Error Objects vs just plain false/null

Question

I’m in the process of writing a plugin and I’m trying to gauge when to use different approaches of handling errors.

There are three methods I’m considering:

  • Throwing an Exception (custom class)
  • Returning an Error Object (extension of WP_Error)
  • Just return null/false

Some situations that I’m considering

  • Trying to get/set an option stored in the Registry that doesn’t exist
  • Passing an invalid value to a method (which should be rare)
  • Calling a method that the class’ overloader cannot resolve to

Suggestions? Since writing a WordPress plugin has some special considerations, I’m not sure whether it would be worth asking this on a general PHP board.

0
Doug Wollison 2 years 2020-12-15T15:10:19-05:00 0 Answers 8 views 0

Leave an answer

Browse
Browse