Thread: Assertion failure taking place of a try-catch

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Assertion failure taking place of a try-catch

    I know I can't create this iterator. What is not clear to me is why the try-catch block is failing to isolate the error and instead I get an assertion failure at runtime.

    (note: vec1 has been properly declared and initiliazed)

    Code:
    /* ... */
    #include <stdexcept>
    /* ... */
    try {
        vector<int>::const_iterator ivec = vec1.begin() - 1;
    } catch (runtime_error err) {
        cout << err.what();
    }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Initialize your iterator with begin(). The reason your try/catch block isn't working is because there was not a runtime exception: you tried to initialize an iterator with some random place in memory which is an out_of_range exception, I think.
    Last edited by whiteflags; 06-11-2006 at 08:32 AM.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, the range_error class is derived from runtime_error. So it should be caught with the main class, no? I do have the same problem if I try to catch it with range_error, mind you.

    I don't want to initialize this iterator correctly. I'm curious now as to why the assertion. My first guess is that this is a variable definition and I couldn't catch it. But moving the definition outside of the try block didn't solve the assertion.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Well, the range_error class is derived from runtime_error.
    No, out_of_range isn't a runtime_error.
    Last edited by whiteflags; 06-11-2006 at 09:05 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the range_error class is derived from runtime_error.
    citizen referred to out_of_range, which is derived from logic_error.
    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
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ah. I understand now.

    Well... I did some further testing with out_of_range and the logic_error base class. To no avail. I'm not sure if this is an implementation issue. MinGW doesn't even report an error. I'm able to output the dereferenced iterator and even increment or decrement it inside the try block without any error being caught. Whereas VC++ 2005 Express throws the assertion.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't think that creating an illegal iterator should throw an exception. As far as I know not even dereferencing an illegal one would throw a range_error. If you want a vector to throw an exception you need to use at().
    like this
    Code:
    #include <iostream>
    #include <vector>
    #include <stdexcept>
    
    using namespace std;
    
    int main() {
        vector<int> vec1;
        try {
            int i = vec1.at(1);        
        } catch ( logic_error err ) {
            cout << err.what() << endl;
        }    
    }
    >> Whereas VC++ 2005 Express throws the assertion.
    guess that is a debug build

    Kurt

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Now that I think about it, why would an exception be thrown from begin()? It's not implemented like that. If you want to catch stuff in your for loop you'd have to throw it yourself. Something like:
    Code:
    try {
              vector<int>::const_iterator it = vec1.begin() - 1;
              if( *it != vec1.begin() )
                 throw logic_error();
              // for goes here
    // I've not tested this but it should be okay...
    Be advised I've always had trouble throwing out_of_range exceptions manually, so maybe just throwing a logic error is better. Kurt is still right by the way. It's where its at()

    Cheesy pun.
    Last edited by whiteflags; 06-11-2006 at 09:57 AM.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. That's it. Thanks all
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 02:22 AM
  3. Assertion Failure w/ sprintf
    By osal in forum Windows Programming
    Replies: 5
    Last Post: 06-14-2004, 04:38 PM
  4. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 09:58 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM