~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty-proposed

« back to all changes in this revision

Viewing changes to yo/exceptions/systemerror.yo

  • Committer: Package Import Robot
  • Author(s): Frank B. Brokken
  • Date: 2013-05-30 13:32:18 UTC
  • mfrom: (1.1.24)
  • Revision ID: package-import@ubuntu.com-20130530133218-k39mr5uredd093jr
Tags: 9.7.2-1
New upstream release, repairs several minor left-over flaws.
This release incorporates 9.7.0 and 9.7.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    A ti(std::system_error) can be thrown when an error occurs that has an
 
2
associated error code. Such errors are typically encountered when calling
 
3
low-level (like operating system) functions.
 
4
 
 
5
Before using tt(system_error) the tthi(system_error) header file must have
 
6
been included. 
 
7
 
 
8
A tt(system_error) object can be constructed using the standard textual
 
9
description of the nature of the encountered error, but in addition accepts an
 
10
emi(error_code) or emi(error_category) object (see the next two sections),
 
11
further specifying the nature of the error. The tt(error_code) and
 
12
tt(error_category) classes are also declared in the tt(system_error) header
 
13
file.
 
14
 
 
15
The header file tt(system_error) also defines an ti(enum class errc)hi(errc)
 
16
whose values are equal to and describe in a less cryptic way the traditional
 
17
error code values as offered by bf(C) macros, e.g.,
 
18
        verb(
 
19
    enum class errc 
 
20
    {
 
21
        address_family_not_supported, // EAFNOSUPPORT
 
22
        address_in_use,               // EADDRINUSE
 
23
        address_not_available,        // EADDRNOTAVAIL
 
24
        already_connected,            // EISCONN
 
25
        argument_list_too_long,       // E2BIG
 
26
        argument_out_of_domain,       // EDOM
 
27
        bad_address,                  // EFAULT
 
28
        ...
 
29
    };
 
30
        )
 
31
 
 
32
In addition to the standard tt(what) member, the tt(system_error) class also
 
33
offers a member tt(code) returning a const reference to the exception's error
 
34
code. Here is the class's public interface:
 
35
        verb(
 
36
class system_error: public runtime_error 
 
37
{
 
38
    public:
 
39
        system_error(error_code ec, string const &what_arg);
 
40
        system_error(error_code ec, char const *what_arg);
 
41
        system_error(error_code ec);
 
42
        system_error(int ev, error_category const &ecat,
 
43
                     string const &what_arg);
 
44
        system_error(int ev, error_category const &ecat,
 
45
                     char const *what_arg);
 
46
        system_error(int ev, error_category const &ecat);
 
47
        error_code const &code() const noexcept;
 
48
        char const *what() const noexcept;
 
49
}
 
50
        )
 
51
 
 
52
The NTBS returned by its tt(what) member may be formatted by a
 
53
tt(system_error) object like this:
 
54
        verb(
 
55
    what_arg + ": " + code().message()
 
56
        )
 
57
 
 
58
Note that, although tt(system_error) was derived from tt(runtime_error),
 
59
you'll lose the tt(code) member when catching a tt(std::exception) object. Of
 
60
course, downcasting is always possible, but that's a stopgap. Therefore, if a
 
61
tt(system_error) is thrown, a matching tt(catch(system_error const &)) clause
 
62
should be provided (for a flexible alternative, see the class 
 
63
    hi(Exception (Bobcat)) tt(FBB::Exception) in the author's 
 
64
        url(Bobcat library)(http://bobcat.sourceforge.net).\
 
65
        hi(Bobcat library)hi(http://bobcat.sourceforge.net))
 
66