File tree Expand file tree Collapse file tree 9 files changed +113
-109
lines changed Expand file tree Collapse file tree 9 files changed +113
-109
lines changed Original file line number Diff line number Diff line change @@ -32,22 +32,24 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
32
32
33
33
```cpp
34
34
// domain_error.cpp
35
- // compile with: /EHsc /GR
35
+ // compile with: /EHsc
36
+ #include <exception>
36
37
#include <iostream>
37
-
38
+ #include <stdexcept>
39
+ #include <typeinfo>
38
40
using namespace std;
39
41
40
- int main( )
42
+ int main()
41
43
{
42
44
try
43
45
{
44
- throw domain_error( "Your domain is in error!" );
46
+ throw domain_error("Your domain is in error!");
45
47
}
46
- catch (exception & e)
48
+ catch (const exception& e)
47
49
{
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
+ }
51
53
}
52
54
/* Output:
53
55
Caught: Your domain is in error!
Original file line number Diff line number Diff line change @@ -29,28 +29,29 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
29
29
## Example
30
30
31
31
```cpp
32
- // invalid_arg .cpp
33
- // compile with: /EHsc /GR
32
+ // invalid_argument .cpp
33
+ // compile with: /EHsc
34
34
#include <bitset>
35
+ #include <exception>
35
36
#include <iostream>
36
-
37
+ #include <typeinfo>
37
38
using namespace std;
38
39
39
- int main( )
40
+ int main()
40
41
{
41
42
try
42
43
{
43
- bitset< 32 > bitset( string( "11001010101100001b100101010110000") );
44
+ bitset<32> b( "11001010101100001b100101010110000");
44
45
}
45
- catch ( exception &e )
46
+ catch (const exception& e )
46
47
{
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
+ }
50
51
}
51
52
/* Output:
52
- Caught invalid bitset<N> char
53
- Type class std::invalid_argument
53
+ Caught: invalid bitset char
54
+ Type: class std::invalid_argument
54
55
*/
55
56
```
56
57
Original file line number Diff line number Diff line change @@ -30,41 +30,29 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
30
30
31
31
```cpp
32
32
// length_error.cpp
33
- // compile with: /EHsc /GR /MDd
34
- #include <vector>
33
+ // compile with: /EHsc
34
+ #include <cstddef>
35
+ #include <exception>
35
36
#include <iostream>
36
-
37
+ #include <typeinfo>
38
+ #include <vector>
37
39
using namespace std;
38
40
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()
53
42
{
54
43
try
55
44
{
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));
58
46
}
59
- catch ( exception &e )
47
+ catch (const exception& e )
60
48
{
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
+ }
64
52
}
65
53
/* Output:
66
- Caught vector<T> too long
67
- Type class std::length_error
54
+ Caught: vector too long
55
+ Type: class std::length_error
68
56
*/
69
57
```
70
58
Original file line number Diff line number Diff line change @@ -30,27 +30,29 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
30
30
31
31
```cpp
32
32
// logic_error.cpp
33
- // compile with: /EHsc /GR
33
+ // compile with: /EHsc
34
+ #include <exception>
34
35
#include <iostream>
36
+ #include <stdexcept>
37
+ #include <typeinfo>
35
38
using namespace std;
36
39
37
- int main( )
40
+ int main()
38
41
{
39
42
try
40
43
{
41
- throw logic_error( "logic error" );
44
+ throw logic_error("Does not compute!" );
42
45
}
43
- catch ( exception &e )
46
+ catch (const exception& e )
44
47
{
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
+ }
48
51
}
49
- ```
50
-
51
- ``` Output
52
- Caught: logic error
52
+ /* Output:
53
+ Caught: Does not compute!
53
54
Type: class std::logic_error
55
+ */
54
56
```
55
57
56
58
## Requirements
Original file line number Diff line number Diff line change @@ -31,29 +31,31 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
31
31
```cpp
32
32
// out_of_range.cpp
33
33
// compile with: /EHsc
34
- #include <string >
34
+ #include <exception >
35
35
#include <iostream>
36
-
36
+ #include <string>
37
+ #include <typeinfo>
37
38
using namespace std;
38
39
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);
45
47
cout << str << endl;
46
48
}
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
+ }
50
54
}
51
- ```
52
-
53
- ## Output
54
-
55
- ``` cpp
55
+ /* Output:
56
56
Caught: invalid string position
57
+ Type: class std::out_of_range
58
+ */
57
59
```
58
60
59
61
## Requirements
Original file line number Diff line number Diff line change @@ -30,30 +30,31 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
30
30
31
31
```cpp
32
32
// overflow_error.cpp
33
- // compile with: /EHsc /GR
33
+ // compile with: /EHsc
34
34
#include <bitset>
35
+ #include <exception>
35
36
#include <iostream>
36
-
37
+ #include <typeinfo>
37
38
using namespace std;
38
39
39
- int main( )
40
+ int main()
40
41
{
41
42
try
42
43
{
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();
47
48
}
48
- catch ( exception &e )
49
+ catch (const exception& e )
49
50
{
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
+ }
53
54
}
54
55
/* Output:
55
- Caught bitset<N> overflow
56
- Type class std::overflow_error
56
+ Caught: bitset overflow
57
+ Type: class std::overflow_error
57
58
*/
58
59
```
59
60
Original file line number Diff line number Diff line change @@ -28,20 +28,24 @@ The value returned by [what](../standard-library/exception-class.md) is a copy o
28
28
29
29
```cpp
30
30
// range_error.cpp
31
- // compile with: /EHsc /GR
31
+ // compile with: /EHsc
32
+ #include <exception>
32
33
#include <iostream>
34
+ #include <stdexcept>
35
+ #include <typeinfo>
33
36
using namespace std;
37
+
34
38
int main()
35
39
{
36
40
try
37
41
{
38
- throw range_error( "The range is in error!" );
42
+ throw range_error("The range is in error!");
39
43
}
40
- catch (range_error & e)
44
+ catch (const exception& e)
41
45
{
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
+ }
45
49
}
46
50
/* Output:
47
51
Caught: The range is in error!
Original file line number Diff line number Diff line change @@ -30,27 +30,28 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
30
30
31
31
```cpp
32
32
// runtime_error.cpp
33
- // compile with: /EHsc /GR
33
+ // compile with: /EHsc
34
+ #include <exception>
34
35
#include <iostream>
35
-
36
+ #include <locale>
37
+ #include <typeinfo>
36
38
using namespace std;
37
39
38
- int main( )
40
+ int main()
39
41
{
40
- // runtime_error
41
42
try
42
43
{
43
- locale loc( "test" );
44
+ locale loc("test");
44
45
}
45
- catch ( exception &e )
46
+ catch (const exception& e )
46
47
{
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
+ }
50
51
}
51
52
/* Output:
52
- Caught bad locale name
53
- Type class std::runtime_error
53
+ Caught: bad locale name
54
+ Type: class std::runtime_error
54
55
*/
55
56
```
56
57
Original file line number Diff line number Diff line change @@ -32,21 +32,24 @@ The value returned by `what()` is a copy of `message.data()`. For more informati
32
32
33
33
```cpp
34
34
// underflow_error.cpp
35
- // compile with: /EHsc /GR
35
+ // compile with: /EHsc
36
+ #include <exception>
36
37
#include <iostream>
37
-
38
+ #include <stdexcept>
39
+ #include <typeinfo>
38
40
using namespace std;
39
41
40
- int main( )
42
+ int main()
41
43
{
42
44
try
43
45
{
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;
45
52
}
46
- catch ( exception &e ) {
47
- cerr << "Caught: " << e.what( ) << endl;
48
- cerr << "Type: " << typeid( e ).name( ) << endl;
49
- };
50
53
}
51
54
/* Output:
52
55
Caught: The number's a bit small, captain!
You can’t perform that action at this time.
0 commit comments