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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:32:57+00:00 2026-06-04T12:32:57+00:00

I have div() in Cal class in my CalTest class has following methods to

  • 0

I have div() in Cal class in my CalTest class has following methods to test the div().

public fucnction div($a,$b){
   if($b == 0){
    throw new Exception("Divided by zero");
   }
   return $a/$b
}

I can pass only testDiv() but testDiv2().

enter image description here

I want to catch check wheather the exeption thrown correctly using PHPUnit. What am I missing here?? Your help is highly appreciated. Thank you!

enter image description 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-04T12:32:58+00:00Added an answer on June 4, 2026 at 12:32 pm

    Just came across the same issue. For some reason PHPUnit will not allow you to set the expectedException to a generic exception and I am not sure why. Personally I opt to throw custom Exception codes rather than have to create a new exception class every time I want to differentiate exceptions.

    Here is how I got around it:

    /**
     * @expectedException Test_Exception
     */
    public function testDivByZero()
    {
        try {
            // Fyi you don't need to do an assert test here, as we are only testing the exception, so just make the call
            $result = $this->object->div(1,0);
        } catch (Exception $e) {
            if ('Exception' === get_class($e)) {
                throw new Test_Exception($e->getMessage(), $e->getCode());
            }
        }
     }
    
     // Test_Exception.php
     class Test_Exception extends Exception
     {
         public function __construct($message = null, $code = 0, Exception $previous = null)
         {
             parent::__construct($message, $code, $previous);
         }
     }
    

    This will allow you to engineer your code the way you want, and throw “generic” exceptions. Basically it just tests the Exception class and if it’s generic, re-wrap it as a different exception; Test_Exception.

    — Update —

    Found out yesterday that they have removed the generic exception restriction in the current “master” branch, which will be 3.7. Apparently the lead engineer has no desire of patching 3.6.

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

Sidebar

Related Questions

I have div which has three buttons as, <div id=buttons> <input id=preview class=ButtonStyle type=submit
I have 31 div with the same class .cal. Is there any way to
I have div s with class someclass; each div has child div s with
can anyone explain me what happen here?? i have this html code: <div class=calculadora>
I have div tag inside body html: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
I have div having class name .modalPopup inside the div I have multiple table
I have div class called .stage which is a stage of a questionnaire <div
I have div tag with css class name. div class name is same in
I have DIV which has 2 backgrounds, in my case, it is gradient and
I have div, which has id 'content'. It's visible. <div id=wrapper style=display:block> <div id=content>

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.