std::out_of_range

std::out_of_range (범위 밖 예외)

컨테이너나 함수에 전달된 인자가 유효한 범위를 벗어났을 때 던져지는 논리 오류 예외예요. 예를 들어 std::vector::at이 범위 밖 인덱스로 호출되면 발생해요.

출처: cppreference

본문

<stdexcept> 헤더에 정의돼 있고, std::logic_error에서 파생되는 예외예요.

class out_of_range : public logic_error;

std::out_of_range는 표현식이 컨테이너나 함수의 유효한 범위 밖의 위치를 참조할 때 던져지는 예외로, std::logic_error에서 파생돼요.

대표적인 발생 지점:

  • std::vector::at, std::array::at, std::string::at, std::deque::at 등 범위 검사 인덱스 접근이 범위를 벗어날 때.
  • std::bitset::operator[](비-const), std::bitset::test가 범위 밖 위치를 참조할 때.
  • std::stoi/stol/stof 등 문자열→숫자 변환이 out-of-range 값일 때.
  • std::string::substr가 시작 위치가 범위를 벗어날 때 등.

std::vector::atpos >= size()이면 std::out_of_range를 던져요.

생성자:

  • explicit out_of_range( const std::string& what_arg );
  • explicit out_of_range( const char* what_arg );

더 알아보기 (Learn more)

cppreference