Thread: Can u correct below code so that invalid_argument standard exception is thrown?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Can u correct below code so that invalid_argument standard exception is thrown?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class A
    {
    int i;int j;
    public: 
    A(int ii,int jj)
    {
      i=ii;
      j=jj;
    
    }
     void fun()
     {
       throw exception();
     }
    
    };
    
    int main()
    {
      try
    {
      A a1("Hi",1); // It shoudl throw invalid_argument exception but gives compile error
      
      }
      catch(invalid_argument&)
      {
        cout<<"Exception caught in main";
      }
    
    
    
    }
    It gives compilation error...can u give me example without stl where invalid_argument excpetion is thrown and caught?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Look at the parameters of the constructor of A and compare them with the arguments that you pass when you invoke the constructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's not how exceptions work. Your class is defined as taking an integer as the first constructor parameter, so it MUST take an integer. You cannot pass a string, and expect the runtime to throw an exception for you.

    The invalid_argument exception should be used when you receive an argument value that you do not expect -- not an invalid type.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    ok. Is it necessary to explicitly throw invaid argument exception or system generates it?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by forumuser
    Is it necessary to explicitly throw invaid argument exception or system generates it?
    If you want to signal an invalid argument that is passed to a function that you write, you should consider throwing std::invalid_argument, but you are not required to do so since it may make more sense to return an error code or use an assertion, depending on the circumstances. Now, what do you mean by the "system"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that you are completely mistaken about C++ works. Whether the types in your code are correct or not is checked statically at compile-time, and not at runtime.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  2. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  3. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  4. Source code of C standard library...
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 06:35 AM
  5. Source code of the standard library functions...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 12:35 PM