Why do i get uncaught exception and the program exits?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danielo
    New Member
    • Nov 2010
    • 8

    Why do i get uncaught exception and the program exits?

    Code:
    class Error {
    Error(string err) { cout << err; }
    }
    void main()
    {
      try {
            throw string("exception");
     }
     catch(Error Err)
    {
    }
    return
    }
    my question is :
    why i get uncaught exception and the program exits ,
    even though i have a Error constructor that gets string ?
    thanks
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    danielo,

    You must excuse me, but I have to point out that your code is completely untested.

    It won't compile due to syntax errors at lines 3 and 12.

    In addition, the main function is supposed to return an int.

    Please fix the syntax errors, as well as the problem with main, and re-post your question.

    Good Luck!
    Oralloy
    Last edited by Oralloy; Nov 30 '10, 11:18 PM. Reason: removed incorrectly formed sentence.

    Comment

    • Ferdinend
      New Member
      • Jul 2010
      • 7

      #3
      Hi Danielo,

      you have not coded to catch a string exception.
      Or you have to code for generic catch.

      Comment

      • danielo
        New Member
        • Nov 2010
        • 8

        #4
        i understand , but i am catching Error class which contains constructor that accepts string.. so i dont understand why not

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          danielo,

          in C++, you catch arbitrary types.

          why do you think that you are supposed to catch an arbitrary Err object that has no formal existance in the specification?

          there is a standard exception class in the std namespace, which standard many C++ exceptions and errors are derived from. however, its use is not mandatory, like java.lang.Throw able is in Java. all the std::exception class does is provide a structured method of building exceptions.

          so...throw a type, catch a type. in your case, you're throwing a string, but catching an Err, so your exception handler will not catch it.

          if you were hoping for implicit type conversion, it's not done as far as I know. there is implicit up-casting, however there is no run-time search over the possible conversion space for an arbitrarily thrown object. your entire example is 13 lines, however C++ is required to allow separate compilation and linking of modules, and still work consistently.

          BTW, did you ever fix your errors and run the thing?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Also you do not catch objects. You catch references or pointers to objects. Otherwise, you create a new object in the catch block which isn't the one that was thrown:

            Code:
            catch (Error& Err)
            //etc...

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              @weaknessforcat s,

              Yep, that's the best way to go about it. However, I don't think that danielo understands the general concept of C++ error handling yet. I know that years ago, when try and catch were first introduced, that I used to throw strings (of the char* sort) on small projects, as they were quick and easy.

              In fact, won't his example fail (assuming he throws an Err), because there is no appropriate copy constructor for his Err object? I don't have a C++ compiler handy, otherwise I'd test for my own benefit, thus my question.

              Thanks,
              Oralloy

              Comment

              • hype261
                New Member
                • Apr 2010
                • 207

                #8
                I don't believe it will fail because the compiler will create a copy constructor for him.

                It won't print out the error string because that piece of logic is only defined in the constructor.

                Comment

                • Oralloy
                  Recognized Expert Contributor
                  • Jun 2010
                  • 988

                  #9
                  @hype261,

                  Sometimes the compiler can create a copy constructor, and other times it can't. I don't remember the rules. However, when there are embedded classes/objects involved, there are problems. I do know that the old C standard where structs are copied bitwise does not apply in this case.

                  Still, as I noted in my comment, I don't have a C++ compiler handy to test against. If you do, will you build and run the test, please?

                  Thanks,
                  Oralloy

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    That's correct. C++ copies memberwise.

                    Therefore, the compiler copy constructor just calls the copy constructor of every member of your class.

                    You can supersede the compiler copy constructor by one of your own if you need more than is offered by the compiler version.

                    Comment

                    • Oralloy
                      Recognized Expert Contributor
                      • Jun 2010
                      • 988

                      #11
                      @weaknessforcat s,

                      Which means that the compiler cannot generate an implicit copy constructor for a class, which contains a member that does not have an accessable copy constructor. Are there other restrictions?

                      In this case, everything's known, so the copy constructor is obviated.

                      However, the exception handling path knows nothing about object copies at run-time, so it can't implicitly instantiate copy constructors, even if it were appropriate.

                      Thus, my next question - does the C++ specification require that the copy constructors be be implicitly implemented for all non-reference-type exception handlers? (Yes, I do realize that invoking copy constructors for primitive types and pointers is moot. These, however are conceptually optimized away, anyway)

                      Comment

                      • danielo
                        New Member
                        • Nov 2010
                        • 8

                        #12
                        i have a big application , i just gave an example of the situation , so i could understand the theory behind it , and for copy constructor , the compiler defines a default copy constructor which will activate the default copy constructor of each member , so no need for me to define one , and as for the exception class in stl (std..) , almost no body uses it because it is not good , i just want to understand why it is ok to catch base class of an exception and why it is illegal to implicit cast ( string to Err) since for example :

                        void func1(Error err);

                        and u make this call :
                        func1(string("t hat works!!!"));

                        this works while for exceptions it doesnt
                        thanks

                        Comment

                        • hype261
                          New Member
                          • Apr 2010
                          • 207

                          #13
                          Orally,

                          I built and ran the program which I believe was your question and it works just fine.

                          Code:
                          class Error 
                          { 
                          public:
                          	Error(std::string err) { std::cout << err; } 
                          }; 
                          
                          
                          
                          int main()
                          {
                          	try
                          	{
                          		throw Error("Help");
                          	}
                          	catch(Error Err)
                          	{
                          
                          	}
                          
                          	return 0;
                          }
                          The output was Help and the compilier did generate the copy constructor for me.

                          Comment

                          • danielo
                            New Member
                            • Nov 2010
                            • 8

                            #14
                            any idea why it wont catch it if you throw a string instead ?

                            Comment

                            • Oralloy
                              Recognized Expert Contributor
                              • Jun 2010
                              • 988

                              #15
                              danielo,

                              It won't catch a string, because the examples we currently have do not have exception handlers that catch string objects.

                              Of course, we can add an exception handler for strings if we want. Extending the example that @hype261 built:
                              Code:
                              class Error  
                              {  
                              public: 
                                  Error(std::string err):str(err) { std::cout << err; }  
                                  string str;
                              };  
                                
                                
                                
                              int main() 
                              { 
                                  try 
                                  { 
                                      throw string("Help");
                                      throw Error("Help"); 
                                  } 
                                  catch(Error Err) 
                                  { 
                                    cout << "Caught Error: " << Err.str << endl;
                                  } 
                                  catch(string const &str) 
                                  { 
                                    cout << "Caught string: " << str << endl;
                                  } 
                                
                                  return 0; 
                              }
                              You'll have to add the appropriate includes, but this should work for you.

                              Comment

                              Working...