forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.dd
213 lines (170 loc) · 7.81 KB
/
errors.dd
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
Ddoc
$(SPEC_S Error Handling,
$(BLOCKQUOTE Julius C'ster,
I came, I coded, I crashed.
)
All programs have to deal with errors. Errors are unexpected conditions that
are not part of the normal operation of a program. Examples of common errors
are:
$(UL
$(LI Out of memory.)
$(LI Out of disk space.)
$(LI Invalid file name.)
$(LI Attempting to write to a read-only file.)
$(LI Attempting to read a non-existent file.)
$(LI Requesting a system service that is not supported.)
)
$(H2 The Error Handling Problem)
The traditional C way of detecting and reporting errors is not traditional,
it is ad-hoc and varies from function to function, including:
$(UL
$(LI Returning a NULL pointer.)
$(LI Returning a 0 value.)
$(LI Returning a non-zero error code.)
$(LI Requiring errno to be checked.)
$(LI Requiring that a function be called to check if the previous
function failed.)
)
$(P To deal with these possible errors, tedious error handling code must be added
to each function call. If an error happened, code must be written to recover
from the error, and the error must be reported to the user in some user friendly
fashion. If an error cannot be handled locally, it must be explicitly
propagated back to its caller.
The long list of errno values needs to be converted into appropriate
text to be displayed. Adding all the code to do this can consume a large part
of the time spent coding a project - and still, if a new errno value is added
to the runtime system, the old code can not properly display a meaningful
error message.)
$(P Good error handling code tends to clutter up what otherwise would be a neat
and clean looking implementation.)
$(P Even worse, good error handling code is itself error prone, tends to be the
least tested (and therefore buggy) part of the project, and is frequently
simply omitted. The end result is likely a "blue screen of death" as the
program failed to deal with some unanticipated error.)
$(P Quick and dirty programs are not worth writing tedious error handling code
for, and so such utilities tend to be like using a table saw with no
blade guards.)
What's needed is an error handling philosophy and methodology such that:
$(UL
$(LI It is standardized - consistent usage makes it more useful.)
$(LI The result is reasonable even if the programmer fails
to check for errors.)
$(LI Old code can be reused with new code without having
to modify the old code to be compatible with new error types.)
$(LI No errors get inadvertently ignored.)
$(LI $(SINGLEQUOTE Quick and dirty) utilities can be written that still
correctly handle errors.)
$(LI It is easy to make the error handling source code look good.)
)
$(H2 The D Error Handling Solution)
Let's first make some observations and assumptions about errors:
$(UL
$(LI Errors are not part of the normal flow of a program. Errors
are exceptional, unusual, and unexpected.)
$(LI Because errors are unusual, execution of error handling code
is not performance critical.)
$(LI The normal flow of program logic is performance critical.)
$(LI All errors must be dealt with in some way, either by
code explicitly written to handle them, or by some system default
handling.)
$(LI The code that detects an error knows more about the error
than the code that must recover from the error.)
)
$(P The solution is to use exception handling to report errors. All
errors are objects derived from abstract class $(D Error). $(D Error)
has a pure virtual function called toString() which produces a $(D char[])
with a human readable description of the error.)
$(P If code detects an error like "out of memory," then an Error is thrown
with a message saying "Out of memory". The function call stack is unwound,
looking for a handler for the Error.
$(DDSUBLINK statement, TryStatement, Finally blocks)
are executed as the
stack is unwound. If an error handler is found, execution resumes there. If
not, the default Error handler is run, which displays the message and
terminates the program.)
$(P How does this meet our criteria?)
$(DL
$(DT It is standardized - consistent usage makes it more useful.)
$(DD This is the D way, and is used consistently in the D
runtime library and examples.)
$(DT The result is reasonable result even if the programmer fails
to check for errors.)
$(DD If no catch handlers are there for the errors, then the
program gracefully exits through the default error handler
with an appropriate message.)
$(DT Old code can be reused with new code without having
to modify the old code to be compatible with new error types.)
$(DD Old code can decide to catch all errors, or only specific ones,
propagating the rest upwards. In any case, there is no more
need to correlate error numbers with messages, the correct message
is always supplied.)
$(DT No errors get inadvertently ignored.)
$(DD Error exceptions get handled one way or another. There is nothing
like a NULL pointer return indicating an error, followed by trying to
use that NULL pointer.)
$(DT 'Quick and dirty' utilities can be written that still
correctly handle errors.)
$(DD Quick and dirty code need not write any error handling code at
all, and don't need to check for errors. The errors will be caught,
an appropriate message displayed, and the program gracefully shut down
all by default.)
$(DT It is easy to make the error handling source code look good.)
$(DD The try/catch/finally statements look a lot nicer than endless
if (error) goto errorhandler; statements.)
)
How does this meet our assumptions about errors?
$(DL
$(DT Errors are not part of the normal flow of a program. Errors
are exceptional, unusual, and unexpected.)
$(DD D exception handling fits right in with that.)
$(DT Because errors are unusual, execution of error handling code
is not performance critical.)
$(DD Exception handling stack unwinding is a relatively slow process.)
$(DT The normal flow of program logic is performance critical.)
$(DD Since the normal flow code does not have to check every
function call for error returns, it can be realistically faster
to use exception handling for the errors.)
$(DT All errors must be dealt with in some way, either by
code explicitly written to handle them, or by some system default
handling.)
$(DD If there's no handler for a particular error, it is handled
by the runtime library default handler. If an error is ignored,
it is because the programmer specifically added code to ignore
an error, which presumably means it was intentional.)
$(DT The code that detects an error knows more about the error
than the code that must recover from the error.)
$(DD There is no more need to translate error codes into human
readable strings, the correct string is generated by the error
detection code, not the error recovery code. This also leads to
consistent error messages for the same error between applications.)
)
Using exceptions to handle errors leads to another issue - how to write
exception safe programs. $(DPLLINK exception-safe.html, Here's how).
$(COMMENT
$(OL
$(LI Programmers, especially inexperienced ones, tend to neglect
to test for the special error return value.
Their code just assumed the function completed successfully.
This leads to erratic and unpredictable
behavior if the function did fail.)
$(LI How each function deals with errors tends to be unique and
inconsistent, leading to more unintended
programmatic errors.)
$(LI How the error gets reported to the user tends to vary
arbitrarily from one program to the next and one
error case to the next.)
$(LI Dealing with error cases causes tedious and error-prone code
to be written, and so can consume much
programming effort.)
$(LI Error handling logic tends to be buggy because it rarely
gets tested by the test team.)
$(LI Functions that should have clean interfaces wind up
cluttering them with error return parameters and
cases.)
)
)
)
Macros:
TITLE=Errors
WIKI=Errors
CATEGORY_SPEC=$0