Skip to content

Commit 31a9ecd

Browse files
Overhaul all stdexcept examples.
* `/GR` is enabled by default. * We don't need `/MDd`. * Include all necessary headers. * Improve paren/brace/bracket consistency. * Add constness when catching `const exception&`. * Rename to `invalid_argument.cpp`, matching the type exactly. * `bitset` can be directly constructed from `const char *`, we don't need `string`. * Shadowing the `bitset` type with a `bitset` variable is confusing; rename to `b`. * Always print colons in `"Caught: "` and `"Type: "` for consistency. * Regenerate all outputs with VS 2022 17.0 Preview 3, some exception messages changed slightly. * Massively simplify the `length_error` example and remove non-Standard code. * Change the `logic_error` string to `"Does not compute!"` to avoid potential confusion. * Drop unnecessary comments like `// out_of_range`. * Always print the type - it was missing from the `out_of_range` example. * Consistently display the output as a comment at the end of the file. * In the `range_error` example, catch `const exception&` to demonstrate inheritance like all other examples.
1 parent 5a2373f commit 31a9ecd

9 files changed

+113
-109
lines changed

docs/standard-library/domain-error-class.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,24 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3232
3333
```cpp
3434
// domain_error.cpp
35-
// compile with: /EHsc /GR
35+
// compile with: /EHsc
36+
#include <exception>
3637
#include <iostream>
37-
38+
#include <stdexcept>
39+
#include <typeinfo>
3840
using namespace std;
3941
40-
int main( )
42+
int main()
4143
{
4244
try
4345
{
44-
throw domain_error( "Your domain is in error!" );
46+
throw domain_error("Your domain is in error!");
4547
}
46-
catch (exception &e)
48+
catch (const exception& e)
4749
{
48-
cerr << "Caught: " << e.what( ) << endl;
49-
cerr << "Type: " << typeid(e).name( ) << endl;
50-
};
50+
cerr << "Caught: " << e.what() << endl;
51+
cerr << "Type: " << typeid(e).name() << endl;
52+
}
5153
}
5254
/* Output:
5355
Caught: Your domain is in error!

docs/standard-library/invalid-argument-class.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,29 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
2929
## Example
3030
3131
```cpp
32-
// invalid_arg.cpp
33-
// compile with: /EHsc /GR
32+
// invalid_argument.cpp
33+
// compile with: /EHsc
3434
#include <bitset>
35+
#include <exception>
3536
#include <iostream>
36-
37+
#include <typeinfo>
3738
using namespace std;
3839
39-
int main( )
40+
int main()
4041
{
4142
try
4243
{
43-
bitset< 32 > bitset( string( "11001010101100001b100101010110000") );
44+
bitset<32> b("11001010101100001b100101010110000");
4445
}
45-
catch ( exception &e )
46+
catch (const exception& e)
4647
{
47-
cerr << "Caught " << e.what( ) << endl;
48-
cerr << "Type " << typeid( e ).name( ) << endl;
49-
};
48+
cerr << "Caught: " << e.what() << endl;
49+
cerr << "Type: " << typeid(e).name() << endl;
50+
}
5051
}
5152
/* Output:
52-
Caught invalid bitset<N> char
53-
Type class std::invalid_argument
53+
Caught: invalid bitset char
54+
Type: class std::invalid_argument
5455
*/
5556
```
5657

docs/standard-library/length-error-class.md

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,29 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3030
3131
```cpp
3232
// length_error.cpp
33-
// compile with: /EHsc /GR /MDd
34-
#include <vector>
33+
// compile with: /EHsc
34+
#include <cstddef>
35+
#include <exception>
3536
#include <iostream>
36-
37+
#include <typeinfo>
38+
#include <vector>
3739
using namespace std;
3840
39-
template<class T>
40-
class stingyallocator : public allocator< T>
41-
{
42-
public:
43-
template <class U>
44-
struct rebind { typedef stingyallocator<U> other; };
45-
_SIZT max_size( ) const
46-
{
47-
return 10;
48-
};
49-
50-
};
51-
52-
int main( )
41+
int main()
5342
{
5443
try
5544
{
56-
vector<int, stingyallocator< int > > myv;
57-
for ( int i = 0; i < 11; i++ ) myv.push_back( i );
45+
vector<int> v(100 + static_cast<size_t>(-1) / sizeof(int));
5846
}
59-
catch ( exception &e )
47+
catch (const exception& e)
6048
{
61-
cerr << "Caught " << e.what( ) << endl;
62-
cerr << "Type " << typeid( e ).name( ) << endl;
63-
};
49+
cerr << "Caught: " << e.what() << endl;
50+
cerr << "Type: " << typeid(e).name() << endl;
51+
}
6452
}
6553
/* Output:
66-
Caught vector<T> too long
67-
Type class std::length_error
54+
Caught: vector too long
55+
Type: class std::length_error
6856
*/
6957
```
7058

docs/standard-library/logic-error-class.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,29 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3030
3131
```cpp
3232
// logic_error.cpp
33-
// compile with: /EHsc /GR
33+
// compile with: /EHsc
34+
#include <exception>
3435
#include <iostream>
36+
#include <stdexcept>
37+
#include <typeinfo>
3538
using namespace std;
3639
37-
int main( )
40+
int main()
3841
{
3942
try
4043
{
41-
throw logic_error( "logic error" );
44+
throw logic_error("Does not compute!");
4245
}
43-
catch ( exception &e )
46+
catch (const exception& e)
4447
{
45-
cerr << "Caught: " << e.what( ) << endl;
46-
cerr << "Type: " << typeid( e ).name( ) << endl;
47-
};
48+
cerr << "Caught: " << e.what() << endl;
49+
cerr << "Type: " << typeid(e).name() << endl;
50+
}
4851
}
49-
```
50-
51-
```Output
52-
Caught: logic error
52+
/* Output:
53+
Caught: Does not compute!
5354
Type: class std::logic_error
55+
*/
5456
```
5557

5658
## Requirements

docs/standard-library/out-of-range-class.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,31 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3131
```cpp
3232
// out_of_range.cpp
3333
// compile with: /EHsc
34-
#include <string>
34+
#include <exception>
3535
#include <iostream>
36-
36+
#include <string>
37+
#include <typeinfo>
3738
using namespace std;
3839
39-
int main() {
40-
// out_of_range
41-
try {
42-
string str( "Micro" );
43-
string rstr( "soft" );
44-
str.append( rstr, 5, 3 );
40+
int main()
41+
{
42+
try
43+
{
44+
string str("Micro");
45+
string rstr("soft");
46+
str.append(rstr, 5, 3);
4547
cout << str << endl;
4648
}
47-
catch ( exception &e ) {
48-
cerr << "Caught: " << e.what( ) << endl;
49-
};
49+
catch (const exception& e)
50+
{
51+
cerr << "Caught: " << e.what() << endl;
52+
cerr << "Type: " << typeid(e).name() << endl;
53+
}
5054
}
51-
```
52-
53-
## Output
54-
55-
```cpp
55+
/* Output:
5656
Caught: invalid string position
57+
Type: class std::out_of_range
58+
*/
5759
```
5860

5961
## Requirements

docs/standard-library/overflow-error-class.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,31 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3030
3131
```cpp
3232
// overflow_error.cpp
33-
// compile with: /EHsc /GR
33+
// compile with: /EHsc
3434
#include <bitset>
35+
#include <exception>
3536
#include <iostream>
36-
37+
#include <typeinfo>
3738
using namespace std;
3839
39-
int main( )
40+
int main()
4041
{
4142
try
4243
{
43-
bitset< 33 > bitset;
44-
bitset[32] = 1;
45-
bitset[0] = 1;
46-
unsigned long x = bitset.to_ulong( );
44+
bitset<33> b;
45+
b[32] = 1;
46+
b[0] = 1;
47+
unsigned long x = b.to_ulong();
4748
}
48-
catch ( exception &e )
49+
catch (const exception& e)
4950
{
50-
cerr << "Caught " << e.what( ) << endl;
51-
cerr << "Type " << typeid( e ).name( ) << endl;
52-
};
51+
cerr << "Caught: " << e.what() << endl;
52+
cerr << "Type: " << typeid(e).name() << endl;
53+
}
5354
}
5455
/* Output:
55-
Caught bitset<N> overflow
56-
Type class std::overflow_error
56+
Caught: bitset overflow
57+
Type: class std::overflow_error
5758
*/
5859
```
5960

docs/standard-library/range-error-class.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,24 @@ The value returned by [what](../standard-library/exception-class.md) is a copy o
2828
2929
```cpp
3030
// range_error.cpp
31-
// compile with: /EHsc /GR
31+
// compile with: /EHsc
32+
#include <exception>
3233
#include <iostream>
34+
#include <stdexcept>
35+
#include <typeinfo>
3336
using namespace std;
37+
3438
int main()
3539
{
3640
try
3741
{
38-
throw range_error( "The range is in error!" );
42+
throw range_error("The range is in error!");
3943
}
40-
catch (range_error &e)
44+
catch (const exception& e)
4145
{
42-
cerr << "Caught: " << e.what( ) << endl;
43-
cerr << "Type: " << typeid( e ).name( ) << endl;
44-
};
46+
cerr << "Caught: " << e.what() << endl;
47+
cerr << "Type: " << typeid(e).name() << endl;
48+
}
4549
}
4650
/* Output:
4751
Caught: The range is in error!

docs/standard-library/runtime-error-class.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,28 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3030
3131
```cpp
3232
// runtime_error.cpp
33-
// compile with: /EHsc /GR
33+
// compile with: /EHsc
34+
#include <exception>
3435
#include <iostream>
35-
36+
#include <locale>
37+
#include <typeinfo>
3638
using namespace std;
3739
38-
int main( )
40+
int main()
3941
{
40-
// runtime_error
4142
try
4243
{
43-
locale loc( "test" );
44+
locale loc("test");
4445
}
45-
catch ( exception &e )
46+
catch (const exception& e)
4647
{
47-
cerr << "Caught " << e.what( ) << endl;
48-
cerr << "Type " << typeid( e ).name( ) << endl;
49-
};
48+
cerr << "Caught: " << e.what() << endl;
49+
cerr << "Type: " << typeid(e).name() << endl;
50+
}
5051
}
5152
/* Output:
52-
Caught bad locale name
53-
Type class std::runtime_error
53+
Caught: bad locale name
54+
Type: class std::runtime_error
5455
*/
5556
```
5657

docs/standard-library/underflow-error-class.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,24 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
3232
3333
```cpp
3434
// underflow_error.cpp
35-
// compile with: /EHsc /GR
35+
// compile with: /EHsc
36+
#include <exception>
3637
#include <iostream>
37-
38+
#include <stdexcept>
39+
#include <typeinfo>
3840
using namespace std;
3941
40-
int main( )
42+
int main()
4143
{
4244
try
4345
{
44-
throw underflow_error( "The number's a bit small, captain!" );
46+
throw underflow_error("The number's a bit small, captain!");
47+
}
48+
catch (const exception& e)
49+
{
50+
cerr << "Caught: " << e.what() << endl;
51+
cerr << "Type: " << typeid(e).name() << endl;
4552
}
46-
catch ( exception &e ) {
47-
cerr << "Caught: " << e.what( ) << endl;
48-
cerr << "Type: " << typeid( e ).name( ) << endl;
49-
};
5053
}
5154
/* Output:
5255
Caught: The number's a bit small, captain!

0 commit comments

Comments
 (0)