terminate called after throwing an instance of 🔧 C/C++

C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check

A C++ exception wasn’t caught anywhere, so std::terminate aborted the program. what() tells you the reason.

Seen on: C/C++

Meaning

Frequent ones: std::out_of_range from .at(), std::invalid_argument from std::stoi on non-numeric text, std::bad_alloc for huge allocations, std::logic_error from basic_string constructed with nullptr.

Common causes

  • vector.at()/string.at() with an invalid index
  • std::stoi/stod on invalid input
  • Allocation failure (bad_alloc)
  • No try/catch around code that throws

⚡ Quick fix

  1. Validate indices and input before calling
  2. Catch the specific exception where you can recover
  3. Run in a debugger with catch throw to find the source

Detailed fix by platform

C++

  1. cpp
    try {
        int n = std::stoi(input);
    } catch (const std::invalid_argument&) {
        std::cerr << "not a number\
    ";
    }

How to diagnose

  1. Type — Exception class and what()
  2. Source — gdb: catch throw

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.