For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > Getting file and line when using std::out_of_range









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Getting file and line when using std::out_of_range
Torben Laursen

2005-11-25, 7:58 am

Hi

I have been using

#define _SECURE_SCL 1
#define _SECURE_SCL_THROWS 1

combined with:

catch(std::out_of_range)

To find errors in my code when using vectors.
It works fine but is there a way to get the file and line that throws?
Since having this information will save me a lot of time.

Thanks

Torben Laursen



Ulrich Eckhardt

2005-11-25, 7:58 am

Torben Laursen wrote:
> #define _SECURE_SCL 1
> #define _SECURE_SCL_THROWS 1
>
> combined with:
>
> catch(std::out_of_range)
>
> To find errors in my code when using vectors.
> It works fine but is there a way to get the file and line that throws?


Don't catch them. I wouldn't have configured that it throws anyway, because
any violation of an index that doesn't throw already (like vector::at()) is
a programming error, a violation of an invariant and therefore an error you
can't recover from - the catch() you mention gives IMHO a false sense of
security.
Anyhow, you can easily get the information about where and when by not
catching the exception - in that case, the debugger is invoked (check the
debugger's exception handling configuration though).

Uli

Torben Laursen

2005-11-25, 7:58 am

Hi Ulrich

Thanks
Yes I only need this information when debugging.
But I must be slow here since I have enabled break for exceptions under
debug/Exceptions and when a exception is thrown the debugger stops the
program and takes me to a file called: dbghook.c. So I do know what part of
my code holds the error.

Torben


"Ulrich Eckhardt" <eckhardt@satorlaser.com> wrote in message
news:mrbj53-55b.ln1@satorlaser.homedns.org...
> Torben Laursen wrote:
>
> Don't catch them. I wouldn't have configured that it throws anyway,
> because
> any violation of an index that doesn't throw already (like vector::at())
> is
> a programming error, a violation of an invariant and therefore an error
> you
> can't recover from - the catch() you mention gives IMHO a false sense of
> security.
> Anyhow, you can easily get the information about where and when by not
> catching the exception - in that case, the debugger is invoked (check the
> debugger's exception handling configuration though).
>
> Uli
>



Ale Contenti [MSFT]

2005-11-28, 7:07 pm

Hi Torben,

You can look in the call stack (to show the callstack, go to Debug | Windows | Call Stack).

For this simple program:

#include <vector>

int main()
{
std::vector<int> v(10);

int i = v[10];
}

The call stack is:

msvcr80d.dll!_crt_debugger_hook(int _Reserved=4294624) Line 62 C
msvcr80d.dll!_invalid_parameter(const wchar_t * pszExpression=0x00418684, const wchar_t * pszFunction=0x004186a8, const wchar_t * pszFile=0x004187e0, unsigned int nLine=757, unsigned int pReserved=0) Line 86 + 0x7 bytes C++
t102.exe!main() Line 7 + 0xa bytes C++
t102.exe!__tmainCRTStartup() Line 586 + 0x19 bytes C
t102.exe!mainCRTStartup() Line 403 C
kernel32.dll!BaseProcessStart(unsigned long (void)* lpStartAddress=0x004111e0) Line 813 + 0x3 bytes C

notice that std::vector<int>::operator[] called _invalid_parameter because the index is out of bounds.

Hope this helps.

Ale Contenti
VC++ Libraries

-----Original Message-----
From: Torben Laursen
Posted At: Friday, November 25, 2005 5:16 AM
Posted To: microsoft.public.vc.language
Conversation: Getting file and line when using std::out_of_range
Subject: Re: Getting file and line when using std::out_of_range


Hi Ulrich

Thanks
Yes I only need this information when debugging.
But I must be slow here since I have enabled break for exceptions under
debug/Exceptions and when a exception is thrown the debugger stops the
program and takes me to a file called: dbghook.c. So I do know what part of
my code holds the error.

Torben


"Ulrich Eckhardt" <eckhardt@satorlaser.com> wrote in message
news:mrbj53-55b.ln1@satorlaser.homedns.org...[color=darkred]
> Torben Laursen wrote:
>
> Don't catch them. I wouldn't have configured that it throws anyway,
> because
> any violation of an index that doesn't throw already (like vector::at())
> is
> a programming error, a violation of an invariant and therefore an error
> you
> can't recover from - the catch() you mention gives IMHO a false sense of
> security.
> Anyhow, you can easily get the information about where and when by not
> catching the exception - in that case, the debugger is invoked (check the
> debugger's exception handling configuration though).
>
> Uli
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com