0

I am rather new to exceptions. In a part of my program if a specific variable goes beyond a certain value I want to manually throw an exception which will say that the variable has gone outside of accepted range. The variable being 16 bits can still hold the "invalid" values but under normal program operation it will only hold a subset of the possible values it can hold.

How do I generate this specific type of exception?

8
  • 1
    Please ask your question your rubber duck and delete it (not the duck) Commented Sep 27, 2016 at 21:38
  • C++ does not have the concept of an "overflow exception". Instead, overflowing integers will silently trigger undefined behavior and/or wrapping. If you want a specific type of exception, you will need to create your own. Commented Sep 27, 2016 at 21:40
  • 1
    Reopened because OP is not trying to detect an actual overflow, but trying to detect some kind of "logical out-of-range" instead. Commented Sep 27, 2016 at 21:46
  • well, if I have to "create my own exception" then I shall just look into it then Commented Sep 27, 2016 at 21:47
  • 1
    Dupe candidate would be stackoverflow.com/questions/148511/… for example. Commented Sep 27, 2016 at 21:48

1 Answer 1

5
throw std::out_of_range("Your value doesn't belong to a valid range");

The class std::out_of_range is defined in

#include <stdexcept>

Documentation: std::out_of_range - cppreference.com

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.