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 7979013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:39:08+00:00 2026-06-04T09:39:08+00:00

I need to learn C++ basics for a research project, and I’m experimenting with

  • 0

I need to learn C++ basics for a research project, and I’m experimenting with error/exception handling. I did use the throw command successfully to anticipate on events that might occur (like divide by zero), but I cannot figure out how to catch unanticipated exceptions. Take this example code:

#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;

void arrayOutOfBound()
{
    int a[3] = {1, 2, 3};

    try
    {
        cout << "This should not display: " << a[5] << endl;
    }
    catch(runtime_error &e)
    /* catch(exception &e)     // also does not work  */
    {
        cout << "Error: " << e.what() << endl;
    }
}

int main()
{
    arrayOutOfBound();
}

I guess I have to use throw statements somewhere, but suppose I really did not know that a[5] wouldn’t work (or the user entered this index and I did not check for the array size), then how can I prevent the program from crashing? (as this happens in the Visual C++ Express 2010 debugger)

Note: if I would do try { int result = a[5]; } first, outside the block, and try to use cout << result at the end, the program does not compile. The compiler is trying to help me, but therefore I cannot try the exception handling.

  • 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-06-04T09:39:10+00:00Added an answer on June 4, 2026 at 9:39 am

    suppose I really did not know that a[5] wouldn’t work (or the user entered this index and I did not check for the array size), then how can I prevent the program from crashing?

    You simply can’t. An out of bounds access to an array causes undefined behavior in C++, it won’t throw an exception. When you are lucky enough, you get a crash.

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

Sidebar

Related Questions

I need to learn prism (for evaluation to use at my work). I am
I want to learn about Cryptography theory (basics) and I want to use the
I need to learn more about creating setup projects from within Visual Studio to
I need to learn RegEx but don't have time to figure this out right
I need to learn C++ in order to learn building Nokia WRT and or
Do I need to learn the WCF Service trace viewer tool? I understand I
Do i need to learn Java for running hadoop in Amazon EC2? I am
I'm lately feeling the need to learn a build tool. I'm looking through StackOverflow
I am a newbie in Obj-C. I need to learn this better so please
I can do a few things with PowerPoint, but I need to learn more

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.