CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #137

What stoi Throws

easyExceptions

std::stoi is called with a normal number, with text, and with a number far too large for an int. What is printed?

📄 Code

1#include <iostream>
2#include <stdexcept>
3#include <string>
4 
5void parse(const std::string& text) {
6 try {
7 int value = std::stoi(text);
8 std::cout << "number " << value << "\n";
9 } catch (const std::invalid_argument&) {
10 std::cout << "not a number: " << text << "\n";
11 } catch (const std::out_of_range&) {
12 std::cout << "too big: " << text << "\n";
13 }
14}
15 
16int main() {
17 parse("42");
18 parse("abc");
19 parse("999999999999999999999");
20 return 0;
21}