Click here to Skip to main content
Sign Up to vote bad
good
Hi,
I am trying to handle exception in the following code
 
CStringArray StrArray;
StrArray.RemoveAll();
try
{
    CString str;
    str = StrArray.ElementAt(1);
}
catch(...)
{
    AfxMessageBox("No Element Present at this Position");
    throw;
}
 
But for the above case instead of catching the assertion of no element present at that position it directly throws an VC++ Built in Debug Assertion of Array Size without entering the catch block.
Note:- I want to execute this code in Debug Mode and not in Release mode
Regards,
Madhukar Parab
Posted 29 Nov '11 - 19:06
Edited 30 Nov '11 - 14:47

Comments
Eduard Lu - 30 Nov '11 - 20:47
EDIT: Added "pre" tag

3 solutions

There is no such thing as Debug or Release mode. These are just names of configurations. The configurations are just used to switch groups of build and other options at once, they are just the names of such groups of options, according the the schema of *.*proj files (which is exactly the same for different types of projects). You can debug your application if it was compiled with either configuration.
 
Before addressing your problem, you should understand those configurations. I hope better understanding of the project and build will help you. As to the problem, please see the options in Visual Studio: [Main menu] => Debug => Exceptions.
 
—SA
  Permalink  
Just check the size before you try to access it using GetUpperBound()
CStringArray arr;
arr.RemoveAll();
 
CString tst;
int i=1; //for testing

if( i<= arr.GetUpperBound())
	tst = arr.ElementAt(i);
 
You can also use GetSize(), just remember that on an empty list that's going to give you zero where GetUpperBound will give you a -1 (size gives you the number of elements where UpperBound gives you the maximum index).
  Permalink  
Comments
Coder Block - 17 Dec '11 - 6:45
that exactly in mentioned in my above scrab yhat its not at all possible for some exception
Albert Holguin - 19 Dec '11 - 9:06
what?
Ignore -SA's comment, of course there are DEBUG and RELEASE modes, the IDE generates both of those for you by defaule, there are DEBUG and RELEASE libraries, which are different, and, of course, tons of Code Project Articles describing the difference between DEBUG and RELEASE modes.
 
And you can create your own "configurations" or muck with the settings for DEBUG and RELEASE configurations enough that they no longer resemble the defaults set by the IDE. However, the default DEBUG and RELEASE mode configurations are so ingrained in development processes that they have taken on a meaning of their own and in our common / communal memory.
 
As to your problem, the DEBUG mode libraries use the assertion macros to catch common problems in calls to these routines *BEFORE* they blow up in unexpected ways and throw exceptions. In debugging, they are meant to help you catch these problems with meaningful messages instead of you catching the "null pointer exception" or other obscure problem that you'd have to investigate.
 
So, while they are helpful in that regard, they do take away your ability to "catch" the exception.
 
Many of these asserts do allow you to "continue", that is, resume the code using the bad value / parameter. Often, doing that will throw the exception you expect to catch. But don't forget that the debugger is trying to help you there too so it may see the exception before you do anyway.
 
Small price to pay for all the other help the debugger gives you.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 325
1 Aarti Meswania 250
2 Sergey Alexandrovich Kryukov 249
3 Slacker007 240
4 Ron Beyer 190
0 Sergey Alexandrovich Kryukov 8,743
1 OriginalGriff 7,124
2 CPallini 3,678
3 Rohan Leuva 3,011
4 Maciej Los 2,403


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 30 Nov 2011
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid