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
- Validate indices and input before calling
- Catch the specific exception where you can recover
- Run in a debugger with catch throw to find the source
Detailed fix by platform
C++
- cpp
try { int n = std::stoi(input); } catch (const std::invalid_argument&) { std::cerr << "not a number\ "; }
How to diagnose
- Type — Exception class and what()
- 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:
Similar errors
Most viewed in C/C++
Other ways to find it
🧠 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026