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

This is a discussion on Can u correct below code so that invalid_argument standard exception is thrown? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; class A { int i;int j; public: A(int ii,int jj) { i=ii; ...

  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
    21,403
    Look at the parameters of the constructor of A and compare them with the arguments that you pass when you invoke the constructor.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,246
    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
    21,403
    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"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:51 AM
  3. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  4. Source code of C standard library...
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 05:35 AM
  5. Source code of the standard library functions...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 11:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21