Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6322247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:17:47+00:00 2026-05-24T16:17:47+00:00

Here is a snippet of the code that creates a run time error Microsoft

  • 0

Here is a snippet of the code that creates a run time error “Microsoft Visual C++ Runtime Library” http://www.flickr.com/photos/66130188@N07/6023459646/

string text = something;
size_t index = text.find("hoopla");
try{
    if(text.at(index-1)<'0'&&text.at(index-1)>'9')
       return false;
}catch(out_of_range){return true;}

I am running it in Qt creator. It is not triggering the catch block. When the program reaching the text.at(index-1) and index-1 is out of bounds, it creates the run time error in Qt http://www.flickr.com/photos/66130188@N07/6023453724/

I did not have a problem when I used MVS2010. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:17:48+00:00Added an answer on May 24, 2026 at 4:17 pm

    You can avoid the exception check entirely by simply checking the return value of find first:

    if ((index == std::string::npos || index == 0)        ||
        (text[index - 1] < '0' && text[index - 1] > '9')    )
    {
      return false;
    }
    

    In the first case, npos, the search string wasn’t found, and in the second case it is right at the beginning of the ambient string so you cannot look at the character before it.

    (This is called “offensive programming”: Don’t check for errors at runtime, but construct your algorithm so that you know that your access is correct. If you will, you can add an assertion assert(index < text.length()); to express your certainty that you are holding a correct value, which will not weigh down your release version.)

    Update: Replaces .at() by [] since we’re sure of ourselves.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run across some VB.NET code that explicitly creates three GUID constants and uses
Could someone please explain function(tx) in the code snippet below, from this page: http://www.webkit.org/demos/sticky-notes/
Here's a snippet of code from within TurboGears 1.0.6: [dispatch.generic(MultiorderGenericFunction)] def run_with_transaction(func, *args, **kw):
Here's a snippet of code from a shell script I have written: for src
Here is a code snippet for an asp:Wizard control I have on an aspx
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
Here is a snippet of the code : HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request.RawUrl); WebRequest.DefaultWebProxy =
here is the relevent code snippet: public static Rand searchCount (int[] x) { int
Here's the relevant code snippet. public static Territory[] assignTerri (Territory[] board, String[] colors) {
Here is a snippet of my code $fp = fsockopen($s['url'], 80, $errno, $errstr, 5);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.