Skip to content

Commit 6ee809a

Browse files
authored
Merge pull request MicrosoftDocs#3361 from StephanTLavavej/stdexcept
Improve `<stdexcept>` documentation and examples
2 parents 61d4022 + a6813d6 commit 6ee809a

9 files changed

+129
-121
lines changed

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
description: "Learn more about: domain_error Class"
33
title: "domain_error Class"
4-
ms.date: "11/04/2016"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::domain_error"]
66
helpviewer_keywords: ["domain_error class"]
77
ms.assetid: a1d8245d-61c2-4d1e-973f-073bd5dd5fa3
88
---
99
# domain_error Class
1010

11-
The class serves as the base class for all exceptions thrown to report a domain error.
11+
The class serves as the base class for all exceptions thrown to report a domain error (as in mathematics, not networking).
1212

1313
## Syntax
1414

@@ -26,26 +26,30 @@ public:
2626
2727
The value returned by `what()` is a copy of `message.data()`. For more information, see [`what`](../standard-library/exception-class.md) and [`data`](../standard-library/basic-string-class.md#data).
2828
29+
`domain_error` isn't thrown by any functions in the Microsoft implementation of the C++ Standard Library, but it might be thrown by third-party libraries or user code.
30+
2931
## Example
3032
3133
```cpp
3234
// domain_error.cpp
33-
// compile with: /EHsc /GR
35+
// compile with: /EHsc
36+
#include <exception>
3437
#include <iostream>
35-
38+
#include <stdexcept>
39+
#include <typeinfo>
3640
using namespace std;
3741
38-
int main( )
42+
int main()
3943
{
4044
try
4145
{
42-
throw domain_error( "Your domain is in error!" );
46+
throw domain_error("Your domain is in error!");
4347
}
44-
catch (exception &e)
48+
catch (const exception& e)
4549
{
46-
cerr << "Caught: " << e.what( ) << endl;
47-
cerr << "Type: " << typeid(e).name( ) << endl;
48-
};
50+
cerr << "Caught: " << e.what() << endl;
51+
cerr << "Type: " << typeid(e).name() << endl;
52+
}
4953
}
5054
/* Output:
5155
Caught: Your domain is in error!

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: invalid_argument Class"
33
title: "invalid_argument Class"
4-
ms.date: "11/04/2016"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::invalid_argument"]
66
helpviewer_keywords: ["invalid_argument class"]
77
ms.assetid: af6c227d-ad7c-4e63-9dee-67af81d83506
@@ -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: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: length_error Class"
33
title: "length_error Class"
4-
ms.date: "11/04/2016"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::length_error"]
66
helpviewer_keywords: ["length_error class"]
77
ms.assetid: d53c46c5-4626-400d-bd76-bf3e1e0f64ae
@@ -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: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: logic_error Class"
33
title: "logic_error Class"
4-
ms.date: "11/04/2016"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::logic_error"]
66
helpviewer_keywords: ["logic_error class"]
77
ms.assetid: b290d73d-94e1-4288-af86-2bb5d71f677a
@@ -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: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: out_of_range Class"
33
title: "out_of_range Class"
4-
ms.date: "11/04/2016"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::out_of_range"]
66
helpviewer_keywords: ["out_of_range class"]
77
ms.assetid: d0e14dc0-065e-4666-9ac9-51e52223c503
@@ -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: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: overflow_error Class"
33
title: "overflow_error Class"
4-
ms.date: "11/04/2016"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::overflow_error"]
66
helpviewer_keywords: ["overflow_error class"]
77
ms.assetid: bae7128d-e36b-4a45-84f1-2f89da441d20
@@ -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: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
description: "Learn more about: range_error Class"
33
title: "range_error Class"
4-
ms.date: "08/14/2018"
4+
ms.date: "09/09/2021"
55
f1_keywords: ["stdexcept/std::range_error"]
66
helpviewer_keywords: ["range_error class"]
77
ms.assetid: 8afb3e88-fc49-4213-b096-ed63d7aea37c
88
---
99
# range_error Class
1010

11-
The class serves as the base class for all exceptions thrown to report a range error.
11+
The class serves as the base class for all exceptions thrown to report a range error (as in mathematics, not iterators).
1212

1313
## Syntax
1414

@@ -22,26 +22,30 @@ public:
2222
2323
## Remarks
2424
25-
The value returned by [what](../standard-library/exception-class.md) is a copy of `message.data`. For more information, see [basic_string::data](../standard-library/basic-string-class.md#data).
25+
The value returned by [what](../standard-library/exception-class.md) is a copy of `message.data()`. For more information, see [basic_string::data](../standard-library/basic-string-class.md#data).
2626
2727
## Example
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!

0 commit comments

Comments
 (0)