• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/quickstart/error_handling.cpp
1
//  Copyright (c) 2007-2012 Hartmut Kaiser
2
//  Copyright (c)      2011 Bryce Lelbach
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
//
8
//  This example is documented in the Manual under the title "Error Handling"
9
#include <hpx/config.hpp>
10
#include <hpx/hpx.hpp>
11
#include <hpx/hpx_init.hpp>
12

13
#include <hpx/iostream.hpp>
14
#include <hpx/modules/runtime_local.hpp>
15

16
//[error_handling_raise_exception
17
void raise_exception()
×
18
{
19
    HPX_THROW_EXCEPTION(
×
20
        hpx::error::no_success, "raise_exception", "simulated error");
21
}
22
HPX_PLAIN_ACTION(raise_exception, raise_exception_action)
×
23
//]
24

25
///////////////////////////////////////////////////////////////////////////////
26
int hpx_main()
×
27
{
28
    {
29
        ///////////////////////////////////////////////////////////////////////
30
        // Error reporting using exceptions
31
        //[exception_diagnostic_information
32
        hpx::cout << "Error reporting using exceptions\n";
×
33
        try
34
        {
35
            // invoke raise_exception() which throws an exception
36
            raise_exception_action do_it;
37
            do_it(hpx::find_here());
×
38
        }
39
        catch (hpx::exception const& e)
×
40
        {
41
            // Print just the essential error information.
42
            hpx::cout << "caught exception: " << e.what() << "\n\n";
×
43

44
            // Print all of the available diagnostic information as stored with
45
            // the exception.
46
            hpx::cout << "diagnostic information:"
×
47
                      << hpx::diagnostic_information(e) << "\n";
×
48
        }
×
49
        hpx::cout << std::flush;
×
50
        //]
51

52
        // Detailed error reporting using exceptions
53
        //[exception_diagnostic_elements
54
        hpx::cout << "Detailed error reporting using exceptions\n";
×
55
        try
56
        {
57
            // Invoke raise_exception() which throws an exception.
58
            raise_exception_action do_it;
59
            do_it(hpx::find_here());
×
60
        }
61
        catch (hpx::exception const& e)
×
62
        {
63
            // Print the elements of the diagnostic information separately.
64
            hpx::cout << "{what}: " << hpx::get_error_what(e) << "\n";
×
65
            hpx::cout << "{locality-id}: " << hpx::get_error_locality_id(e)
×
66
                      << "\n";
×
67
            hpx::cout << "{hostname}: " << hpx::get_error_host_name(e) << "\n";
×
68
            hpx::cout << "{pid}: " << hpx::get_error_process_id(e) << "\n";
×
69
            hpx::cout << "{function}: " << hpx::get_error_function_name(e)
×
70
                      << "\n";
×
71
            hpx::cout << "{file}: " << hpx::get_error_file_name(e) << "\n";
×
72
            hpx::cout << "{line}: " << hpx::get_error_line_number(e) << "\n";
×
73
            hpx::cout << "{os-thread}: " << hpx::get_error_os_thread(e) << "\n";
×
74
            hpx::cout << "{thread-id}: " << std::hex
×
75
                      << hpx::get_error_thread_id(e) << "\n";
×
76
            hpx::cout << "{thread-description}: "
×
77
                      << hpx::get_error_thread_description(e) << "\n";
×
78
            hpx::cout << "{state}: " << std::hex << hpx::get_error_state(e)
×
79
                      << "\n";
×
80
            hpx::cout << "{stack-trace}: " << hpx::get_error_backtrace(e)
×
81
                      << "\n";
×
82
            hpx::cout << "{env}: " << hpx::get_error_env(e) << "\n";
×
83
        }
×
84
        hpx::cout << std::flush;
×
85
        //]
86

87
        ///////////////////////////////////////////////////////////////////////
88
        // Error reporting using error code
89
        {
90
            //[error_handling_diagnostic_information
91
            hpx::cout << "Error reporting using error code\n";
×
92

93
            // Create a new error_code instance.
94
            hpx::error_code ec;
95

96
            // If an instance of an error_code is passed as the last argument while
97
            // invoking the action, the function will not throw in case of an error
98
            // but store the error information in this error_code instance instead.
99
            raise_exception_action do_it;
100
            do_it(hpx::find_here(), ec);
×
101

102
            if (ec)
×
103
            {
104
                // Print just the essential error information.
105
                hpx::cout << "returned error: " << ec.get_message() << "\n";
×
106

107
                // Print all of the available diagnostic information as stored with
108
                // the exception.
109
                hpx::cout << "diagnostic information:"
×
110
                          << hpx::diagnostic_information(ec) << "\n";
×
111
            }
112

113
            hpx::cout << std::flush;
×
114
            //]
115
        }
116

117
        // Detailed error reporting using error code
118
        {
119
            //[error_handling_diagnostic_elements
120
            hpx::cout << "Detailed error reporting using error code\n";
×
121

122
            // Create a new error_code instance.
123
            hpx::error_code ec;
124

125
            // If an instance of an error_code is passed as the last argument while
126
            // invoking the action, the function will not throw in case of an error
127
            // but store the error information in this error_code instance instead.
128
            raise_exception_action do_it;
129
            do_it(hpx::find_here(), ec);
×
130

131
            if (ec)
×
132
            {
133
                // Print the elements of the diagnostic information separately.
134
                hpx::cout << "{what}: " << hpx::get_error_what(ec) << "\n";
×
135
                hpx::cout << "{locality-id}: " << hpx::get_error_locality_id(ec)
×
136
                          << "\n";
×
137
                hpx::cout << "{hostname}: " << hpx::get_error_host_name(ec)
×
138
                          << "\n";
×
139
                hpx::cout << "{pid}: " << hpx::get_error_process_id(ec) << "\n";
×
140
                hpx::cout << "{function}: " << hpx::get_error_function_name(ec)
×
141
                          << "\n";
×
142
                hpx::cout << "{file}: " << hpx::get_error_file_name(ec) << "\n";
×
143
                hpx::cout << "{line}: " << hpx::get_error_line_number(ec)
×
144
                          << "\n";
×
145
                hpx::cout << "{os-thread}: " << hpx::get_error_os_thread(ec)
×
146
                          << "\n";
×
147
                hpx::cout << "{thread-id}: " << std::hex
×
148
                          << hpx::get_error_thread_id(ec) << "\n";
×
149
                hpx::cout << "{thread-description}: "
×
150
                          << hpx::get_error_thread_description(ec) << "\n\n";
×
151
                hpx::cout << "{state}: " << std::hex << hpx::get_error_state(ec)
×
152
                          << "\n";
×
153
                hpx::cout << "{stack-trace}: " << hpx::get_error_backtrace(ec)
×
154
                          << "\n";
×
155
                hpx::cout << "{env}: " << hpx::get_error_env(ec) << "\n";
×
156
            }
157

158
            hpx::cout << std::flush;
×
159
            //]
160
        }
161

162
        // Error reporting using lightweight error code
163
        {
164
            //[lightweight_error_handling_diagnostic_information
165
            hpx::cout << "Error reporting using an lightweight error code\n";
×
166

167
            // Create a new error_code instance.
168
            hpx::error_code ec(hpx::throwmode::lightweight);
169

170
            // If an instance of an error_code is passed as the last argument while
171
            // invoking the action, the function will not throw in case of an error
172
            // but store the error information in this error_code instance instead.
173
            raise_exception_action do_it;
174
            do_it(hpx::find_here(), ec);
×
175

176
            if (ec)
×
177
            {
178
                // Print just the essential error information.
179
                hpx::cout << "returned error: " << ec.get_message() << "\n";
×
180

181
                // Print all of the available diagnostic information as stored with
182
                // the exception.
183
                hpx::cout << "error code:" << ec.value() << "\n";
×
184
            }
185

186
            hpx::cout << std::flush;
×
187
            //]
188
        }
189
    }
190

191
    // Initiate shutdown of the runtime system.
192
    return hpx::finalize();
×
193
}
194

195
///////////////////////////////////////////////////////////////////////////////
196
int main(int argc, char* argv[])
×
197
{
198
    return hpx::init(argc, argv);    // Initialize and run HPX.
×
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc