try-throw-catch

This is a discussion on try-throw-catch within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by anon Code: void FileHandling::ReadFile(const string& filename) throw( int ) Again you are saying you would throw int's. ...

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by anon View Post
    Code:
    void FileHandling::ReadFile(const string& filename) throw(int)
    Again you are saying you would throw int's. Either don't use a throw specification at all or be honest about it. The compiler won't forgive.

    Concerning error specifications, you are using new which might throw std::bad_alloc, so the specs are wrong.

    Instead of allocating memory with new you should use a std::vector, or otherwise you'll leak the allocated memory when the function throws (or handle that clean-up manually - your choice).
    You're right , but not thoroughly. I use new only after those two first throws, and the error happens on the first throw, before using new...

  2. #32
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Daved View Post
    The throw specification is the part at the end of the function declaration:
    Code:
    void ReadFile(const string& filename) throw(int);
    I just meant to remove those, since those aren't mandatory and you can still use try/catch/throw without them (and many would suggest that you not use them).
    I already tried changing it. (#27) and it didn't work...

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Did you try removing it? Like this:
    Code:
    void ReadFile(const string& filename);

  4. #34
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Daved View Post
    Did you try removing it? Like this:
    Code:
    void ReadFile(const string& filename);
    Yep. Still, the same problem. While performing throw it says

    Unhandled exception at 0x00435768 in HW0.exe: 0xC0000005: Access violation reading location 0xccccccc0.

    and shows me dbgdel.cpp, with a line contains "_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));"

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I tried running the code and found the problem. The issue was the same as the crash I mentioned earlier when you create a FileHandling object but don't call ReadFile. When you exit from ReadFile early (before the dynamic array is created) you get the same problem, it causes a crash when the FileHandling object is destroyed.

    The reason is that you don't initialize vec, so when the destructor tries to delete it then it tries to call delete on a garbage memory location. The best solution of course is to use the vector class. But if you aren't allowed to, then just initialize vec to 0 in the constructor and the delete will not cause any problems if it is never set to another value.

    Also, if you must keep the dynamic array, you should disable copying so that you don't get crashes when your class is copied.

  6. #36
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by Daved View Post
    I tried running the code and found the problem. The issue was the same as the crash I mentioned earlier when you create a FileHandling object but don't call ReadFile. When you exit from ReadFile early (before the dynamic array is created) you get the same problem, it causes a crash when the FileHandling object is destroyed.

    The reason is that you don't initialize vec, so when the destructor tries to delete it then it tries to call delete on a garbage memory location. The best solution of course is to use the vector class. But if you aren't allowed to, then just initialize vec to 0 in the constructor and the delete will not cause any problems if it is never set to another value.

    Also, if you must keep the dynamic array, you should disable copying so that you don't get crashes when your class is copied.
    You little... :-)))
    Oh man...you're the best. It's 1:20Am here, I hardly see, almost lost hope and you did it. You've got no idea how much I thank you.

  7. #37
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Another thing: you use delete, but new[]. You must match the array and non-array forms of new and delete correctly. If you allocate with new[], you must deallocate with delete[].
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #38
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by CornedBee View Post
    Another thing: you use delete, but new[]. You must match the array and non-array forms of new and delete correctly. If you allocate with new[], you must deallocate with delete[].
    Correct! Thanks!

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Another note about throw(type):
    Visual Studio doesn't support throw(type), so rather you should use throw(...) if anything at all. Unless you don't want your code to compile with Visual Studio.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The code will compile fine, it will just generate warnings. If you want exception specifications for whatever reason, then you'll have no problems porting your code to Visual Studio.

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    throw(int) doesn't work.
    throw(...) does, though.
    Visual Studio doesn't support throw(type) specifications, for whatever reason...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #42
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It compiles and runs for me (VC++ 7.1). Not sure why you think it doesn't work.

  13. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    OK, so that's weird because I'm sure it didn't work in 2005.
    In 2008 I just get a warning that the compiler ignores it because it's not implemented.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exceptions "try, catch, and throw Statements" ???
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2008, 10:22 PM
  2. Throw Catch for External Program
    By dav_mt in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2008, 10:52 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Try...catch...throw or ifs?
    By Kylecito in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2006, 10:41 PM
  5. try catch throw
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2003, 03:15 AM

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