How to Throw Out of Range Exception in C++
In C++, we can handle exceptions using try-catch
blocks. We can also throw an exception explicitly using the throw
statement.
This article discusses how to throw an out-of-range exception in C++.
Throw Out of Range Exception in C++
To throw an out-of-range exception in C++, you can create an exception object. For this, you can use the out_of_range()
constructor.
The out_of_range()
constructor is defined in the standard C++ library. It takes a string object as its input argument and returns an out-of-range exception.
You can throw the exception using the throw
statement, as shown in the following example.
#include <iostream>
int main() {
int number = 100;
if (number > 50) {
throw std::out_of_range("I am an exception.");
}
return 0;
}
Output:
terminate called after throwing an instance of 'std::out_of_range'
what(): I am an exception.
Aborted
Here, we have first created a variable named number
. Then, we have checked if the value in the number
is greater than 50
.
If yes, we have thrown an out-of-range exception defined in the standard C++ library.
You can also handle the out-of-range exception using try-catch
blocks. We will throw the exception in the try
block.
We will catch the out-of-range exception and print that we have handled the exception in the catch
block. You can observe this in the following example.
#include <iostream>
int main() {
int number = 100;
try {
if (number > 50) {
throw std::out_of_range("I am an exception.");
}
} catch (std::out_of_range) {
std::cout << "I am here. Exception caught and handled.";
}
return 0;
}
Output:
I am here. Exception caught and handled.
The above output shows that the program has terminated normally after handling the out-of-range exception. On the other hand, the previous program was aborted because we did not handle the exception.
Error While Throwing Out of Range Exception in C++
It is sometimes possible to write a program to throw an out-of-range exception in C++, as shown below.
#include <iostream>
int main() {
int number = 100;
try {
if (number > 50) {
throw std::out_of_range;
}
} catch (std::out_of_range) {
std::cout << "I am here. Exception caught and handled.";
}
return 0;
}
When we run the above code, the program will run into an error with the following execution trace.
/tmp/EG06BKsTcd.cpp: In function 'int main()':
/tmp/EG06BKsTcd.cpp:8:36: error: expected primary-expression before ';' token
8 | throw std::out_of_range;
| ^
The error occurs because we have thrown std::out_of_range
instead of an out-of-range exception object. The std::out_of_range
constructor is a type definition used to create an out-of-range exception.
Hence, you cannot throw it using a throw
statement. To throw an out of range exception in C++, you must create an out of range exception by passing a string as an input argument to the std::out_of_range()
constructor; only then will you be able to throw the out of range exception successfully in C++.
Aditya Raj is a highly skilled technical professional with a background in IT and business, holding an Integrated B.Tech (IT) and MBA (IT) from the Indian Institute of Information Technology Allahabad. With a solid foundation in data analytics, programming languages (C, Java, Python), and software environments, Aditya has excelled in various roles. He has significant experience as a Technical Content Writer for Python on multiple platforms and has interned in data analytics at Apollo Clinics. His projects demonstrate a keen interest in cutting-edge technology and problem-solving, showcasing his proficiency in areas like data mining and software development. Aditya's achievements include securing a top position in a project demonstration competition and gaining certifications in Python, SQL, and digital marketing fundamentals.
GitHub