Unhandled exception

This is a discussion on Unhandled exception within the C++ Programming forums, part of the General Programming Boards category; Here is the relevant code: Code: string getVal(vector<vector<string>> * matrix, int row, int col) { row--;//account for offsets col--; try ...

  1. #1
    Registered User Mirage's Avatar
    Join Date
    Dec 2010
    Posts
    6

    Unhandled exception

    Here is the relevant code:
    Code:
    string getVal(vector<vector<string>> * matrix, int row, int col)
    {
      row--;//account for offsets
      col--;
      try
      {
        if(row >= 0 && col >= 0 && col < cols && row < rows)//make sure it is inbound
          return (*matrix)[row][col];
        else
          throw "Out of Bounds Error!";
      }
      catch (const char * ex)
      {
        cout << "\n*** " << ex << " ***" << endl;
      }
    }
    The boundaries
    cols is 4
    rows is 4

    and row is 5, col is 3
    It catches the error and prints out *** Out of Bounds Error!***
    but then a window pops up saying

    Unhandled exception at 0x62907a8b ...
    Access violation reading location 0xc0e8013e

    I am running this in MS visual studio 2008
    Last edited by Mirage; 12-13-2010 at 07:10 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you printed an error message. What string are you returning, now?

  3. #3
    Registered User Mirage's Avatar
    Join Date
    Dec 2010
    Posts
    6
    Quote Originally Posted by tabstop View Post
    So you printed an error message. What string are you returning, now?
    Thanks man
    I guess I never specified what to return, and that was the problem.
    I've added a return NULL; right after the print statement but this opens up a new problem

    now I have to do checks for NULL, which I don't mind, but now when I call the method like this
    Code:
     string str = getVal(&matrix, 5,3);
      if(str == NULL)
    I get this error
    error C2784: 'bool std:perator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'std::string'

    even if I simplify it to this
    Code:
     string str = "asdf";
      if(str == NULL)
    I get the same errors only when it its directly

    Code:
     string str = "asdf";
      if("asdf" == NULL)
    does it compile

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    I suggest that you #include <stdexcept> and write it in this way:
    Code:
    string getVal(const vector<vector<string>>& matrix, int row, int col)
    {
        // account for offsets
        --row;
        --col;
    
        // make sure it is within bound
        if (row < 0 || col < 0 || col >= cols || row >= rows)
        {
            throw out_of_range("Out of Bounds Error!");
        }
    
        return matrix[row][col];
    }
    Now, to call it would be something like this:
    Code:
    try
    {
        string str = getVal(matrix, row, col);
        // ...
    }
    catch (const out_of_range& ex)
    {
        cout << "\n*** " << ex.what() << " ***" << endl;
    }
    Quote Originally Posted by Mirage
    I've added a return NULL; right after the print statement but this opens up a new problem
    That is not really correct. You might return an empty string, but not NULL, which is zero. The point of my example is that I don't have to cater for this, because if the arguments are out of range, the exception is throw, rather than thrown and then caught at the same place, leading to a path of control where nothing is returned despite the return type not being void.
    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

  5. #5
    Registered User Mirage's Avatar
    Join Date
    Dec 2010
    Posts
    6
    thanks for the help,
    just wondering but you changed the pointer parameter to a reference for readability issues, right?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by Mirage
    just wondering but you changed the pointer parameter to a reference for readability issues, right?
    Yes, and also because a null pointer is not a valid argument anyway.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:30 AM
  2. Unhandled exception in file read
    By ronrardin in forum C Programming
    Replies: 4
    Last Post: 04-09-2010, 10:26 AM
  3. Replies: 3
    Last Post: 11-11-2006, 05:46 PM
  4. unhandled exception error
    By trends in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2002, 05:54 PM
  5. Unhandled Exception
    By StringQuartet13 in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2002, 04:18 PM

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