Assertion failure taking place of a try-catch

This is a discussion on Assertion failure taking place of a try-catch within the C++ Programming forums, part of the General Programming Boards category; I know I can't create this iterator. What is not clear to me is why the try-catch block is failing ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510

    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();
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    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
    Portugal
    Posts
    7,510
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    > 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
    21,931
    Well, the range_error class is derived from runtime_error.
    citizen referred to out_of_range, which is derived from logic_error.
    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
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    ZuK
    ZuK is offline
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    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
    Portugal
    Posts
    7,510
    Yes. That's it. Thanks all
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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, 03:47 AM
  2. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 01: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

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