exception on string (out_of_range)

E

eric

Dear Advanced C++ programers:

from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
Sequence
---------
#include <iostream>
#include <vector>
#include <exception>

using namespace std;

int main() {

char carr[] = {'a', 'b', 'c', 'd', 'e'};

cout << carr[100000] << '\n'; // Whoops, who knows what's going
// to happen
vector<char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
v.push_back('e');

try {
cout << v.at(10000) << '\n'; // at checks bounds and throws
} catch(out_of_range& e) { // out_of_range if it's invalid
cerr << e.what() << '\n';
}
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ‘int main()’:
4-7.cpp:20:11: error: expected type-specifier before ‘out_of_range’
4-7.cpp:20:23: error: expected ‘)’ before ‘&’ token
4-7.cpp:20:23: error: expected ‘{’ before ‘&’ token
4-7.cpp:20:25: error: ‘e’ was not declared in this scope
4-7.cpp:20:26: error: expected ‘;’ before ‘)’ token
 
M

Michael DOUBEZ

Dear Advanced C++ programers:

  from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
Sequence
---------
#include <iostream>
#include <vector>
#include <exception>

using namespace std; [snip]
  } catch(out_of_range& e) {       // out_of_range if it's invalid
    cerr << e.what() << '\n';
  }}

----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ‘int main()’:
4-7.cpp:20:11: error: expected type-specifier before ‘out_of_range’
4-7.cpp:20:23: error: expected ‘)’ before ‘&’ token
4-7.cpp:20:23: error: expected ‘{’ before ‘&’ token
4-7.cpp:20:25: error: ‘e’ was not declared in this scope
4-7.cpp:20:26: error: expected ‘;’ before ‘)’ token

Missing:

#include <stdexcept>
 
F

FÖLDY Lajos

Dear Advanced C++ programers:

from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
Sequence
---------
#include <iostream>
#include <vector>
#include <exception>

using namespace std;

int main() {

char carr[] = {'a', 'b', 'c', 'd', 'e'};

cout << carr[100000] << '\n'; // Whoops, who knows what's going
// to happen
vector<char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
v.push_back('e');

try {
cout << v.at(10000) << '\n'; // at checks bounds and throws
} catch(out_of_range& e) { // out_of_range if it's invalid
cerr << e.what() << '\n';
}
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ?int main()?:
4-7.cpp:20:11: error: expected type-specifier before ?out_of_range?
4-7.cpp:20:23: error: expected ?)? before ?&? token
4-7.cpp:20:23: error: expected ?{? before ?&? token
4-7.cpp:20:25: error: ?e? was not declared in this scope
4-7.cpp:20:26: error: expected ?;? before ?)? token
-------------------------------------------------------------------------------------
the code can be download from
http://examples.oreilly.com/9780596007614/
that mean it was tested success in some platform(vc++ in xp), so
please help, thanks a lot in advance, Eric


Try:

....
} catch(exception& e) { // out_of_range if it's invalid
....


regards,
Lajos
 
E

eric

Dear Advanced C++ programers:
 from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
Sequence
using namespace std;
int main() {
 char carr[] = {'a', 'b', 'c', 'd', 'e'};
 cout << carr[100000] << '\n';    // Whoops, who knows what's going
                                  // to happen
 vector<char> v;
 v.push_back('a');
 v.push_back('b');
 v.push_back('c');
 v.push_back('d');
 v.push_back('e');
 try {
    cout << v.at(10000) << '\n';  // at checks bounds and throws
 } catch(out_of_range& e) {       // out_of_range if it's invalid
   cerr << e.what() << '\n';
 }
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ?int main()?:
4-7.cpp:20:11: error: expected type-specifier before ?out_of_range?
4-7.cpp:20:23: error: expected ?)? before ?&? token
4-7.cpp:20:23: error: expected ?{? before ?&? token
4-7.cpp:20:25: error: ?e? was not declared in this scope
4-7.cpp:20:26: error: expected ?;? before ?)? token
-------------------------------------------------------------------------------------
the code can be download from
http://examples.oreilly.com/9780596007614/
that mean it was tested success in some platform(vc++ in xp), so
please help, thanks a lot in advance, Eric

Try:

...
   } catch(exception& e) {       // out_of_range if it's invalid
...

regards,
Lajos
 
E

eric

Dear Advanced C++ programers:
 from book (c++ cookbook), chapter 4, section 3: Storing Strings ina
Sequence
---------
#include <iostream>
#include <vector>
#include <exception>
using namespace std;
int main() {
 char carr[] = {'a', 'b', 'c', 'd', 'e'};
 cout << carr[100000] << '\n';    // Whoops, who knows what's going
                                  // to happen
 vector<char> v;
 v.push_back('a');
 v.push_back('b');
 v.push_back('c');
 v.push_back('d');
 v.push_back('e');
 try {
    cout << v.at(10000) << '\n';  // at checks bounds and throws
 } catch(out_of_range& e) {       // out_of_range if it's invalid
   cerr << e.what() << '\n';
 }
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ?int main()?:
4-7.cpp:20:11: error: expected type-specifier before ?out_of_range?
4-7.cpp:20:23: error: expected ?)? before ?&? token
4-7.cpp:20:23: error: expected ?{? before ?&? token
4-7.cpp:20:25: error: ?e? was not declared in this scope
4-7.cpp:20:26: error: expected ?;? before ?)? token
-------------------------------------------------------------------------------------
the code can be download from
http://examples.oreilly.com/9780596007614/
that mean it was tested success in some platform(vc++ in xp), so
please help, thanks a lot in advance, Eric

...
   } catch(exception& e) {       // out_of_range if it's invalid
...
regards,
Lajos

---------------------------------------------------------------------------------------------------

Thanks both Lajos and Mike Doubez's suggestions , and it can compile
now by following your suggestion
but, it did not run as expected

I modify a little bit of original program to test where is that
"Segmentation fault" generate
-----------
#include <iostream>
#include <vector>
// #include <exception>
#include <stdexcept>

using namespace std;

int main() {
cout << "go throu 1 \n";
char carr[] = {'a', 'b', 'c', 'd', 'e'};
cout << "go throu 2 \n";
// cout << carr[100000] << '\n'; // Whoops, who knows what's going
// to happen
vector<char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
v.push_back('e');
cout << "go throu 3 \n";
try {
cout << carr[100000] << '\n';
cout << "go throu 4 \n";
cout << v.at(10000) << '\n'; // at checks bounds and throws
// } catch(out_of_range& e) { // out_of_range if it's invalid
} catch(exception& e) {
/* I tried both above , they all produce same error */
cerr << e.what() << " This is out of range\n";
}
}
---------------------------------------------------------
my system response
-----------
../a.out
go throu 1
go throu 2
go throu 3
Segmentation fault
 
F

FÖLDY Lajos

Dear Advanced C++ programers:
 from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
Sequence
using namespace std;
int main() {
 char carr[] = {'a', 'b', 'c', 'd', 'e'};
 cout << carr[100000] << '\n';    // Whoops, who knows what's going
                                  // to happen
 vector<char> v;
 v.push_back('a');
 v.push_back('b');
 v.push_back('c');
 v.push_back('d');
 v.push_back('e');
 try {
    cout << v.at(10000) << '\n';  // at checks bounds and throws
 } catch(out_of_range& e) {       // out_of_range if it's invalid
   cerr << e.what() << '\n';
 }
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ?int main()?:
4-7.cpp:20:11: error: expected type-specifier before ?out_of_range?
4-7.cpp:20:23: error: expected ?)? before ?&? token
4-7.cpp:20:23: error: expected ?{? before ?&? token
4-7.cpp:20:25: error: ?e? was not declared in this scope
4-7.cpp:20:26: error: expected ?;? before ?)? token
-------------------------------------------------------------------------------------
the code can be download from
http://examples.oreilly.com/9780596007614/
that mean it was tested success in some platform(vc++ in xp), so
please help, thanks a lot in advance, Eric

Try:

...
   } catch(exception& e) {       // out_of_range if it's invalid
...

regards,
Lajos
---------------------------------------------------------------------------------------------------

Thanks both Lajos and Mike Doubez's suggestions , and it can compile
now by following your suggestion
but, it did not run as expected

carr[100000] may be too far off. Try with carr[10] and v.at(10).

regards,
Lajos
 
E

eric

 from book (c++ cookbook), chapter 4, section 3: Storing Strings ina
Sequence
---------
#include <iostream>
#include <vector>
#include <exception>
using namespace std;
int main() {
 char carr[] = {'a', 'b', 'c', 'd', 'e'};
 cout << carr[100000] << '\n';    // Whoops, who knows what's going
                                  // to happen
 vector<char> v;
 v.push_back('a');
 v.push_back('b');
 v.push_back('c');
 v.push_back('d');
 v.push_back('e');
 try {
    cout << v.at(10000) << '\n';  // at checks bounds and throws
 } catch(out_of_range& e) {       // out_of_range if it's invalid
   cerr << e.what() << '\n';
 }
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ?int main()?:
4-7.cpp:20:11: error: expected type-specifier before ?out_of_range?
4-7.cpp:20:23: error: expected ?)? before ?&? token
4-7.cpp:20:23: error: expected ?{? before ?&? token
4-7.cpp:20:25: error: ?e? was not declared in this scope
4-7.cpp:20:26: error: expected ?;? before ?)? token
-------------------------------------------------------------------------------------
the code can be download from
http://examples.oreilly.com/9780596007614/
that mean it was tested success in some platform(vc++ in xp), so
please help, thanks a lot in advance, Eric
Try:
...
   } catch(exception& e) {       // out_of_range if it's invalid
...
regards,
Lajos ---------------------------------------------------------------------------------------------------

Thanks both Lajos and Mike Doubez's suggestions , and it can compile
now by following your suggestion
but, it did not run as expected

carr[100000] may be too far off. Try with carr[10] and v.at(10).

regards,
Lajos

yes, carr[100000]
so by the doc of stdexcept
http://www.cplusplus.com/reference/std/stdexcept/out_of_range/
I tried 2 additional errors
length_error
range_error

but result is still (Segmentation fault)
Do you know exactly which error(exception) my (carr[100000]) cause?

Eric
 
F

FÖLDY Lajos

why my catch cann't catch (cout << carr[100000] << '\n';)
's
out of range error?

operator[] does not check the arguments for out-of-range error.
Member function at() does. This is the main difference between them.

regards,
Lajos
 
G

Gert-Jan de Vos

 from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
Sequence
---------
#include <iostream>
#include <vector>
#include <exception>
using namespace std;
int main() {
 char carr[] = {'a', 'b', 'c', 'd', 'e'};
 cout << carr[100000] << '\n';    // Whoops, who knows what'sgoing
                                 // to happen
 vector<char> v;
 v.push_back('a');
 v.push_back('b');
 v.push_back('c');
 v.push_back('d');
 v.push_back('e');
 try {
    cout << v.at(10000) << '\n';  // at checks bounds and throws
 } catch(out_of_range& e) {       // out_of_range if it's invalid
   cerr << e.what() << '\n';
 }
}
----------------------------------------------------------------------
my g++ response
-----------------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
4-7.cpp: In function ?int main()?:
4-7.cpp:20:11: error: expected type-specifier before ?out_of_range?
4-7.cpp:20:23: error: expected ?)? before ?&? token
4-7.cpp:20:23: error: expected ?{? before ?&? token
4-7.cpp:20:25: error: ?e? was not declared in this scope
4-7.cpp:20:26: error: expected ?;? before ?)? token
-------------------------------------------------------------------------------------
the code can be download from
http://examples.oreilly.com/9780596007614/
that mean it was tested success in some platform(vc++ in xp), so
please help, thanks a lot in advance, Eric
Try:
...
   } catch(exception& e) {       // out_of_range if it's invalid
...
regards,
Lajos
carr[100000] may be too far off. Try with carr[10] and v.at(10).
regards,
Lajos

yes, carr[100000]
so by the doc of stdexcepthttp://www.cplusplus.com/reference/std/stdexcept/out_of_range/
I tried 2 additional errors
length_error
range_error

but result is still (Segmentation fault)
Do you know exactly which error(exception) my (carr[100000]) cause?

Eric

carr is (as the name suggests) a plain C style array. These will never
throw exceptions. Out of bound indexing a C array is UB which in your
case translates to Segmentation fault. std::vector and std::string
implement next to operator[], also the at() functions which adds the
extra bound checks and throws an out_of_range exception when the check
fails.

Net all programs can afford or need the checks for every index,
therefore the programmer is offered a choice.

Gert-Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top