Using exception

Thread Starter

mentaaal

Joined Oct 17, 2005
451
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.

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

Mark44

Joined Nov 26, 2007
628
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
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
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:
Rich (BB code):
#include <stdexcept>
using namespace std;

class outofrange : public runtime_error
{
public:
    outofrange()
        :runtime_error("Subscript out of range") {}
};
range_checker() function:
Rich (BB code):
template<typename T>
bool attendance::range_check(const vector<T>&V, int location) const
{
    try
    {
        if (V.size() < static_cast<unsigned int>(location) +1)
            throw outofrange();
        else return true;
    }
    catch (outofrange &error)
    {
        cerr << "Error occurred: " << error.what() << endl << "location was: " << location << endl;
        return false;
    }
 

Thread Starter

mentaaal

Joined Oct 17, 2005
451
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.
 
Top