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

  • Home
  • SEARCH
  • 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 6708949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:49:18+00:00 2026-05-26T07:49:18+00:00

I am having touble testing the throw command after the if (command[0] == ‘(‘)).

  • 0

I am having touble testing the throw command after the if (command[0] == ‘(‘)). The if statment I think means that if the first char in command does not equel ) throw an error. I have tried a several statments without a ) but have still not been able to execute the throw command. Any ideas.

private double ParseTerm(ref string command)
    {
        double returnValue=0;
        if (command.Length != 0)
        {
         if (command[0] == '('))
            {
                command = command.Substring(1,command.Length -1);   // skip the open paren
                returnValue= ParseExpr(ref command);
                if (command[0] != ')')                              // make sure there is a close paren for each open parenthesis
                    throw new System.FormatException();
                command = command.Substring(1,command.Length -1);   // skip the close paren
            } 
        return returnValue;
    }

Here is ParseExpr

private double ParseExpr(ref string command)
    {
        double op, op2;

        if (command == "")                              // Handle the empty expression case
            return 0;

        op = ParseFactor(ref command);                  // parse left side of expression

        if (command != "")                              // if a right side exists, parse it
        {               

            if (command[0] == '+')                      // test for '+'
            {           
                command = command.Substring(1,command.Length -1);   // skip to +

                if (command.Length == 0)    
                    throw new System.FormatException();     // no right hand side operator

                op2 = ParseExpr(ref command);               // parse remainder of the expression
                op +=  op2;
            } 
            else if (command[0] == '-')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseExpr(ref command);           
                op -=  op2;
            } 
        }
        return op;
    }


    private double ParseFactor(ref string command)
    {
        double op, op2;
        op = ParseExp(ref command);
        if (command != "")
        {               
            if (command[0] == '*')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseFactor(ref command);         
                op *=  op2;
            } 
            else if (command[0] == '/' || command[0] == '\\')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseFactor(ref command);     

                if (op2 == 0)                                   // don't allow divide 0
                    throw new System.DivideByZeroException();   // the division operation won't return
                op /=  op2;                                     // throw the exception since we are using doubles
            }               
            else if (command[0] == '%')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseFactor(ref command);                             
                op = (int)op % (int)op2;
            } 
        }
        return op;
    }
  • 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-26T07:49:18+00:00Added an answer on May 26, 2026 at 7:49 am

    You mean a UnitTest? If yes:

    [TestMethod]
    [ExpectedException(typeof(FormatException))]
    public void ParseTerm_when_the_last_char_is_not_a_close_parenthesis_should_throw_FormatException()
    {
        //Call the method here:
        ParseTerm("(some string without close parenthesis");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How is that for a lot of acronyms! I am having trouble testing GWT's
I'm having trouble unit testing a method that changes some properties of a reference
I am having trouble testing equality on the Views that are set up in
I am using NHibernate and Rhinomocks and having trouble testing what I want. I
I'm having trouble figuring out how to get the testing framework set up and
I'm fond of the idea of unit testing but I'm having trouble applying it
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
I'm having some trouble unit testing the model returned by DefaultModelBinder. I want to
I was having trouble determining what was cause my form to not submit when
We've having trouble deploying a web service that works in our development environment, but

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.