-
Notifications
You must be signed in to change notification settings - Fork 33
/
18.05.cpp
63 lines (61 loc) · 1.52 KB
/
18.05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* Exercise 18.5: Modify the following main function to catch any of the
* exception types shown in Figure 18.1 (p. 783):
*
* int main() {
* // use of the C++ standard library
* }
*
* The handlers should print the error message associated with the exception
* before calling abort (defined in the header cstdlib) to terminate main.
*
* By Faisal Saadatmand
*/
#include <cstdlib>
#include <iostream>
#include <exception>
#include <stdexcept>
int main()
{
try {
throw std::runtime_error("error");
// use of the C++ library
} catch (std::length_error &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::out_of_range &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::invalid_argument &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::domain_error &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::logic_error &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::range_error &e){
std::cout << e.what() << std::endl;
abort();
} catch (std::underflow_error &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::overflow_error &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::runtime_error &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::bad_cast &e) {
std::cout << e.what() << std::endl;
abort();
} catch (std::bad_alloc &e) {
std::cout << e.what() << std::endl;
abort();
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
abort();
}
return 0;
}