Standard Exceptions Demo
Understand the standard exceptions available in the C++ language.
We'll cover the following
Problem
Write a program that creates a vector
of 10
elements, and sets up a value in it at a position index
, received from the keyboard. Also, resize the vector
to a new bigger size. Create a bitset
of 5
bits and set it up with a binary string received from the keyboard. Create another bitset
, set it up with 0s
and 1s
, and convert this string
into an unsigned long int
. While carrying out all these operations take care of exceptional conditions that may occur during execution.
Sample runs
Here’s what you should see when you run the program.
// First Run
Vector index: 4
Length error: vector::_M_default_append
String containing 0s and 1s: 0101
// Second Run
Enter vector index:
20
Out of Range error: vector::_M_range_check: __n (which is 20) >= this->size() (which is 10)
Enter a string containing 0s and 1s:
1102
Invalid argument: bitset::_M_copy_from_ptr
Coding solution
Here is a solution to the above problem. You may change the values of index
on line 11
and str
on line 29
to see the output for valid and invalid values of these variables.
Get hands-on with 1400+ tech skills courses.