Using exception

Discussion in 'Programmer's Corner' started by mentaaal, Apr 14, 2009.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys, I am trying to learn about exceptions and thought that I would include an exception handler in a homework assignment. Basically I have made a function range_checker which returns a bool value if the vector subscipt is within range or not.

    I use the function to check for subscript range on int and string vectors. The functions works perfectly when the int vector subscipt is in and out of range and also when the string subscript is within range.

    When i use the range_check function for bounds checking on a subscipt that is out of range, the program goes nuts and loads of text appears in the program window. The alarm bell (beep) also sounds continuously.

    I have included just the function for simplicity but if anything else is required just shout.

    Code ( (Unknown Language)):
    1. template<typename T>
    2. bool attendance::range_check(const vector<T>&V, int location) const
    3. {
    4.     try
    5.     {
    6.         if (V.size() < static_cast<unsigned int>(location) +1)
    7.             throw exception("Out of range");
    8.         else return true;
    9.     }
    10.     catch (exception &error)
    11.     {
    12.         cerr << "Error occurred: " << error.what() << endl << "location was: " << location << endl;
    13.         return false;
    14.     }
    15. }
    The error given by the compiler is:
    Code ( (Unknown Language)):
    1. 'Attendance.exe': Loaded 'Z:\College\Year2\Semester 2\Programming\Attendance\debug\Attendance.exe', Symbols loaded.
    2. 'Attendance.exe': Loaded 'C:\Windows\System32\ntdll.dll', No symbols loaded.
    3. 'Attendance.exe': Loaded 'C:\Windows\System32\kernel32.dll', No symbols loaded.
    4. 'Attendance.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.debugcrt_1fc8b3b9a1e18e3b_8.0.50727.762_none_24c8a196583ff03b\msvcp80d.dll', No symbols loaded.
    5. 'Attendance.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc80.debugcrt_1fc8b3b9a1e18e3b_8.0.50727.762_none_24c8a196583ff03b\msvcr80d.dll', No symbols loaded.
    6. 'Attendance.exe': Loaded 'C:\Windows\System32\msvcrt.dll', No symbols loaded.
    7. First-chance exception at 0x767c42eb in Attendance.exe: Microsoft C++ exception: std::exception at memory location 0x0012fa88..
    8. The program '[2376] Attendance.exe: Native' has exited with code 0 (0x0).
    9.  
     
    Last edited: Apr 14, 2009
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I'm not speaking from much experience with using exceptions in C++; I'm more knowledgeable about exceptions in C# and in .NET Framework. Having said that, I would recommend narrowing down the exceptions you accept in your catch block. As it is now, every exception that is thrown makes its way to your catch exception handler. A place to start looking is here: http://msdn.microsoft.com/en-us/library/x057540h.aspx.
    Mark
     
  3. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hi Mark and thank you for the advise. Well i have made the necessary adjustments so that the catcher will only catch the thrown out of range error.

    I have made a derived class outofrange and have included this class as well as the ammendments made to the rangechecker function. Unfortunately the problem is exactly the same.

    range_checker.h:
    Code ( (Unknown Language)):
    1. #include <stdexcept>
    2. using namespace std;
    3.  
    4. class outofrange : public runtime_error
    5. {
    6. public:
    7.     outofrange()
    8.         :runtime_error("Subscript out of range") {}
    9. };
    range_checker() function:
    Code ( (Unknown Language)):
    1. template<typename T>
    2. bool attendance::range_check(const vector<T>&V, int location) const
    3. {
    4.     try
    5.     {
    6.         if (V.size() < static_cast<unsigned int>(location) +1)
    7.             throw outofrange();
    8.         else return true;
    9.     }
    10.     catch (outofrange &error)
    11.     {
    12.         cerr << "Error occurred: " << error.what() << endl << "location was: " << location << endl;
    13.         return false;
    14.     }
     
  4. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Ok well i have finally found the problem. It was a stupid one and has nothing to do with using exceptions to be honest. The problem was that the return type for the get_module_names function was const string & when the subscript was out of bounds the function was returning an address to a locally defined variable which was obviously being destroyed once the function's job was completed, thus causing the problem.

    Thanks for all the help in resolving the problem anyway.
     
Loading...