Javatpoint Logo
Javatpoint Logo

out of range exception C++

An exception is a runtime error that tampers with the regular instructions that a program follows. It is an undesirable occurrence that is not anticipated to happen during the program's typical execution.

One of the common scenarios where an out-of-range exception occurs is when accessing elements of an array or a container using an index that is less than zero or greater than or equal to the size of the data structure. In C++, we can issue exceptions using the throw keyword and handle them using the try-catch blocks.

The try-catch block is the cornerstone of exception management. Anywhere in the code where an exception might occur is surrounded by a try-catch block.

When the C++ compiler detects an issue with the program, the try keyword is used to cause an exception to be thrown. The try keyword indicates the section of the code where that particular problem occurred, while the catch keyword catches the exceptions.

Programs can throw the "Out of range" exception, which is a common one. The vector, deque, string, and bitset modules in the standard library can additionally throw exceptions when they are outside of their expected range.

If any value that is not included in the expected values is supplied to the method, an exception known as "out of range" is thrown. Knowing the reason behind exceptions is aided by it.

An "out of range" exception in C++ is raised when an attempt is made to access an element outside the permitted range or bounds of a data structure, such as an array, vector, string, or other container. The C++ Standard Library's std::out_of_range exception class is commonly used to represent this type of exception. The following is the theory behind the C++ out-of-range exception:

1. std::out of range Exceptional class:

  • The C++ predefined exception class std::out_of_range is derived from the std::logic_error base class.
  • When an attempt is made to access an element or carry out an operation outside of the permitted range, errors are handled using this technique.

2. Common Causes:

  • Attempting to access an element outside the bounds of an array.
  • Using a negative index or an index greater than or equal to the array size.
  • Using an index that exceeds the size of a std::vector or other containers.
  • Using an iterator that points beyond the valid range of elements.
  • Reaching a nonexistent position when trying to access a container element.
  • Using a container's at function and offering an out-of-range index.
  • Using std::string::at() with an incorrect index to try and extract characters from a string.

3. In handling:

  • You can use a try-catch block to catch a std::out_of_range exception and manage it appropriately by handling it. It frequently entails putting up an error message and carrying out remedial measures.

4. Preventing:

  • It is a good idea to check a container's limits or size before accessing any of its elements to prevent out-of-range exceptions. If you want to make sure the access is inside allowed boundaries, you can use functions like size() and empty().

Example:

Let's take an example to illustrate the use of std::out_of_range in C++:

Output:

Out of Range error: vector::_M_range_check: __n (which is 5) >= this->size() (which is 3)

Explanation:

In this case, the std::out_of_range exception is thrown when an attempt is made to use myVector.at(5) to access an element at index 5 of the vector. This exception is caught and handled in the catch block.

  1. The header files required for input/output operations (\iostream>), vector usage (<vector>), and exception management (<stdexcept>) are included using the #include directives.
  2. The program's entrance point serves as its main()
  3. std::vector{int> myVector = {1, 2, 3}; it creates a vector called myVector with the three integer members 1, 2, and 3.
  4. The code where an exception could be thrown is enclosed in a try block. The at() function is used to attempt to access the element of the vector that is located at index 5, but since the vector doesn't have an element at that position, it will fail with a std::out_of_range
  5. The thrown std::out_of_range exception is caught by the catch block. The type of exception to catch is specified in the const std::out_of_range &e section, where e is the variable that holds the captured exception.
  6. The what() method is used in the catch block to output an error message with a description of the exception to the standard error stream (std::cerr).
  7. return 0; tells the main function to stop because the program has finished running successfully.

Next Topicstrcoll() in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA