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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:12:21+00:00 2026-06-14T02:12:21+00:00

I have few custom exceptions in PHP: class MainException extends Exception {}; class ExceptionOne

  • 0

I have few custom exceptions in PHP:

class MainException extends Exception {};
class ExceptionOne extends MainException {};
class ExceptionTwo extends MainException {};

And I’m using them in my class in two simple methods:

public function firstFunction($param) {
    if ($some_condition) {
        // do whatever
    } else {
        throw new ExceptionOne();
    }
}

public function secondFunction($param) {
    if ($some_condition) {
        // do whatever
    } else {
        throw new ExceptionTwo();
    }
}

I also have a PHPUnit test for both of exceptions, similar to this:

public function testFirstException() {
    try {
        // anything
    } catch (Exception $e) {
        $this->assertType('ExceptionOne', $e);
        $this->assertType('MainException', $e);
    }
}
public function testSecondException() {
    try {
        // anything
    } catch (Exception $e) {
        $this->assertType('ExceptionTwo', $e);
        $this->assertType('MainException', $e);
    }
}

If I test my class in browser and intentionally make my functions fail (with same stuff as in PHPUnit test) I can see ExceptionOne and ExceptionTwo are raised whenever needed. However, when I test it with PHPUnit I always get a failure:

1) testSecondException(SomeTest)
Failed asserting that <PHPUnit_Framework_ExpectationFailedException> is an instance of class "ExceptionTwo".
C:\test.php:67

Line 67 is
$this->assertType('ExceptionTwo', $e);
and it fails here no matter what I try. I’m pretty sure that my condition in secondFunction works correct. The first test (testFirstException) works perfectly and I never get a failure like in the second one.
I’d like to add that I shouldn’t change PHPUnit test here!

What am I doing wrong here??

  • 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-06-14T02:12:23+00:00Added an answer on June 14, 2026 at 2:12 am

    From your comments to cbuckley’s excellent answer, is appears that the test was written like this:

    public function testFirstException() {
        try {
            // some code that is supposed to throw ExceptionOne
            $this->assertTrue(false, "Test failed");
        } catch (Exception $e) {
            $this->assertType('ExceptionOne', $e);
            $this->assertType('MainException', $e);
        }
    }
    

    The assertTrue is used to make sure the test fails if the exception isn’t thrown during the test. However, this doesn’t work because the failed assertion throws a different exception type which causes the error message to be confusing.

    Failed asserting that <PHPUnit_Framework_ExpectationFailedException> 
    is an instance of class "ExceptionOne".
    

    You can fix this by using either @expectedException or setExpectedException. Not only will the test pass when ExceptionOne is thrown, but it will fail when it isn’t thrown or some other exception type is thrown.

    /**
     * @expectedException ExceptionOne
     */
    public function testFirstException() {
        // some code that is supposed to throw ExceptionOne
    }
    

    When no exception is thrown the error message is

    Failed asserting that exception of type "ExceptionOne" is thrown.
    

    and when different type of exception is thrown you’ll see

    Failed asserting that exception of type "Exception" matches
    expected exception "RuntimeException".
    

    This method of testing exceptions is

    • easier to write,
    • easier to read,
    • easier to debug when the test fails,
    • and correct.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have subclassed the UICollectionViewLayoutAttributes class and added a few custom properties. In my
I have a few custom UITableViewCells - http://img11.imageshack.us/i/customfacilitiescell.png/ which are added to this UIViewController
In many of our projects I have seen a few custom collection / or
I have few controls (Label, Custom Textbox, Datagridview) docked on a form. When I
So I have built quite a few custom controls and I have been having
In a word, how? I have a view controller that creates a few custom
I have a few entities which are managed by custom NSManagedObject classes. They are
I have made up a few different custom cells in my UITableView in Interface
I've created a custom adapter implementation. I also have a few different view types
I have a few custom tables in my Drupal database that were created manually.

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.