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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:16:55+00:00 2026-05-18T20:16:55+00:00

Consider a method which might throw an exception with some descriptive text: if ($someCondition)

  • 0

Consider a method which might throw an exception with some descriptive text:

if ($someCondition) {
    throw new \Whatever\Exception('dilithium exhausted');
}

And elsewhere in the method is another block that might throw the same exception, but with different text:

if ($anotherCondition) {
    throw new \Whatever\Exception('differentialator exploded');
}

While writing unit tests for this class, you create failure cases so that you can verify that these two exceptions get thrown properly. In these failure cases, do you prefer to:

A) Use @exceptionExpected in the test method’s docblock to trap the generic \Whatever\Exception class and subsequently ignore the getMessage() text, assuming you got the right one? (Seems like a bad idea.)

or:

B) Use try/catch and then assert that the caught exception’s getMessage() text equals the exact descriptive string you’re expecting? (More resilient but it means changing your tests whenever you change your error wording.)

or:

C) Create a separate exception for each error case (e.g., \Whatever\DilithiumException and \Whatever\DifferentialatorException) and then use @exceptionExpected for each one.

I’m currently using B but tending toward C. I’m curious what others are doing in this same scenario. Do you have any guidelines that help you determine, “At what point does an error deserve its own exception class versus a more generic shared one?”

  • 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-18T20:16:56+00:00Added an answer on May 18, 2026 at 8:16 pm

    All of the above.

    A is great, and I use as much as possible because it is simplest. There is another case when A does not work:

    /**
     * @exceptionExpected FooException
     */
    test() {
      // code that could throw FooException
      ...
      // purpose of the test that throws of FooException
    }
    

    In this case, the test could pass when it should have failed because it didn’t even get to what I was testing. A good way to deal with this is to use $this->setExpectedException()

    B is great when you might actually use information from the exception. Rather than using the text of the exception message I would prefer to use the code. I have a form validation exception that packages up all the problems encountered in the data into one exception. By extending the exception class it becomes easy to transmit a good deal of information from the internal error state to the external handling code.

    C accomplishes the same thing as B, but allows for simplifying the code by relying on more classes. The difference between these two is subtle and I tend to rely on design aesthetic to make the decision.

    TL; DR: Use exception codes rather than messages, and design to the use case rather than the unit tests.

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

Sidebar

Related Questions

Out of the following queries, which method would you consider the better one? What
What would you consider to be the best exception type to throw when an
Consider a private method which is called from JNI and not used otherwise, generating
I have some basic questions about core data (which I am new to) and
Consider the following method signatures: public fooMethod (Foo[] foos) { /*...*/ } and public
Consider a hypothetical method of an object that does stuff for you: public class
Consider type like this one public interface IHaveGenericMethod { T1 Method<T1>(T1 parm); T2 Method<T1,T2>(T1
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList
Consider this problem: I have a program which should fetch (let's say) 100 records
Which method is very effective and simple to code to attain this functionality ?

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.