Hello Programmers!

How can I throw an exception if an unsigned int variable overcomes a limit (that is set by myself). I want the exception to terminate all processes. Thanks!

Hey,

Something like this:

#include <iostream>

const std::size_t limit = 10; 

int main(int argc, char *argv[]) {
    unsigned int numb;

    try {

        std::cout << "Please enter a number: ";
        std::cin >> numb; 

        if(numb > limit)
           throw 1;
    }
    catch(...)
    {
        std::cout << "Out of bounds";
    }


}

// input 100
// output: Out of bounds
// input 1
// true 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.