1Printexc(3) OCaml library Printexc(3)
2
3
4
6 Printexc - Facilities for printing exceptions and inspecting current
7 call stack.
8
10 Module Printexc
11
13 Module Printexc
14 : sig end
15
16
17 Facilities for printing exceptions and inspecting current call stack.
18
19
20
21
22
23 type t = exn = ..
24
25
26 The type of exception values.
27
28
29
30 val to_string : exn -> string
31
32
33 Printexc.to_string e returns a string representation of the exception e
34 .
35
36
37
38 val print : ('a -> 'b) -> 'a -> 'b
39
40
41 Printexc.print fn x applies fn to x and returns the result. If the
42 evaluation of fn x raises any exception, the name of the exception is
43 printed on standard error output, and the exception is raised again.
44 The typical use is to catch and report exceptions that escape a func‐
45 tion application.
46
47
48
49 val catch : ('a -> 'b) -> 'a -> 'b
50
51
52 Printexc.catch fn x is similar to Printexc.print , but aborts the pro‐
53 gram with exit code 2 after printing the uncaught exception. This
54 function is deprecated: the runtime system is now able to print
55 uncaught exceptions as precisely as Printexc.catch does. Moreover,
56 calling Printexc.catch makes it harder to track the location of the
57 exception using the debugger or the stack backtrace facility. So, do
58 not use Printexc.catch in new code.
59
60
61
62 val print_backtrace : out_channel -> unit
63
64
65 Printexc.print_backtrace oc prints an exception backtrace on the output
66 channel oc . The backtrace lists the program locations where the
67 most-recently raised exception was raised and where it was propagated
68 through function calls.
69
70 If the call is not inside an exception handler, the returned backtrace
71 is unspecified. If the call is after some exception-catching code
72 (before in the handler, or in a when-guard during the matching of the
73 exception handler), the backtrace may correspond to a later exception
74 than the handled one.
75
76
77 Since 3.11.0
78
79
80
81 val get_backtrace : unit -> string
82
83
84 Printexc.get_backtrace () returns a string containing the same excep‐
85 tion backtrace that Printexc.print_backtrace would print. Same restric‐
86 tion usage than Printexc.print_backtrace .
87
88
89 Since 3.11.0
90
91
92
93 val record_backtrace : bool -> unit
94
95
96 Printexc.record_backtrace b turns recording of exception backtraces on
97 (if b = true ) or off (if b = false ). Initially, backtraces are not
98 recorded, unless the b flag is given to the program through the OCAML‐
99 RUNPARAM variable.
100
101
102 Since 3.11.0
103
104
105
106 val backtrace_status : unit -> bool
107
108
109 Printexc.backtrace_status() returns true if exception backtraces are
110 currently recorded, false if not.
111
112
113 Since 3.11.0
114
115
116
117 val register_printer : (exn -> string option) -> unit
118
119
120 Printexc.register_printer fn registers fn as an exception printer. The
121 printer should return None or raise an exception if it does not know
122 how to convert the passed exception, and Some s with s the resulting
123 string if it can convert the passed exception. Exceptions raised by the
124 printer are ignored.
125
126 When converting an exception into a string, the printers will be
127 invoked in the reverse order of their registrations, until a printer
128 returns a Some s value (if no such printer exists, the runtime will use
129 a generic printer).
130
131 When using this mechanism, one should be aware that an exception back‐
132 trace is attached to the thread that saw it raised, rather than to the
133 exception itself. Practically, it means that the code related to fn
134 should not use the backtrace if it has itself raised an exception
135 before.
136
137
138 Since 3.11.2
139
140
141
142
143 Raw backtraces
144 type raw_backtrace
145
146
147 The abstract type raw_backtrace stores a backtrace in a low-level for‐
148 mat, instead of directly exposing them as string as the get_backtrace()
149 function does.
150
151 This allows delaying the formatting of backtraces to when they are
152 actually printed, which may be useful if you record more backtraces
153 than you print.
154
155 Raw backtraces cannot be marshalled. If you need marshalling, you
156 should use the array returned by the backtrace_slots function of the
157 next section.
158
159
160 Since 4.01.0
161
162
163
164 val get_raw_backtrace : unit -> raw_backtrace
165
166
167 Printexc.get_raw_backtrace () returns the same exception backtrace that
168 Printexc.print_backtrace would print, but in a raw format. Same
169 restriction usage than Printexc.print_backtrace .
170
171
172 Since 4.01.0
173
174
175
176 val print_raw_backtrace : out_channel -> raw_backtrace -> unit
177
178 Print a raw backtrace in the same format Printexc.print_backtrace uses.
179
180
181 Since 4.01.0
182
183
184
185 val raw_backtrace_to_string : raw_backtrace -> string
186
187 Return a string from a raw backtrace, in the same format Print‐
188 exc.get_backtrace uses.
189
190
191 Since 4.01.0
192
193
194
195 val raise_with_backtrace : exn -> raw_backtrace -> 'a
196
197 Reraise the exception using the given raw_backtrace for the origin of
198 the exception
199
200
201 Since 4.05.0
202
203
204
205
206 Current call stack
207 val get_callstack : int -> raw_backtrace
208
209
210 Printexc.get_callstack n returns a description of the top of the call
211 stack on the current program point (for the current thread), with at
212 most n entries. (Note: this function is not related to exceptions at
213 all, despite being part of the Printexc module.)
214
215
216 Since 4.01.0
217
218
219
220
221 Uncaught exceptions
222 val set_uncaught_exception_handler : (exn -> raw_backtrace -> unit) ->
223 unit
224
225
226 Printexc.set_uncaught_exception_handler fn registers fn as the handler
227 for uncaught exceptions. The default handler prints the exception and
228 backtrace on standard error output.
229
230 Note that when fn is called all the functions registered with at_exit
231 have already been called. Because of this you must make sure any output
232 channel fn writes on is flushed.
233
234 Also note that exceptions raised by user code in the interactive
235 toplevel are not passed to this function as they are caught by the
236 toplevel itself.
237
238 If fn raises an exception, both the exceptions passed to fn and raised
239 by fn will be printed with their respective backtrace.
240
241
242 Since 4.02.0
243
244
245
246
247 Manipulation of backtrace information
248 These functions are used to traverse the slots of a raw backtrace and
249 extract information from them in a programmer-friendly format.
250
251 type backtrace_slot
252
253
254 The abstract type backtrace_slot represents a single slot of a back‐
255 trace.
256
257
258 Since 4.02
259
260
261
262 val backtrace_slots : raw_backtrace -> backtrace_slot array option
263
264 Returns the slots of a raw backtrace, or None if none of them contain
265 useful information.
266
267 In the return array, the slot at index 0 corresponds to the most recent
268 function call, raise, or primitive get_backtrace call in the trace.
269
270 Some possible reasons for returning None are as follow:
271
272 -none of the slots in the trace come from modules compiled with debug
273 information ( -g )
274
275 -the program is a bytecode program that has not been linked with debug
276 information enabled ( ocamlc -g )
277
278
279
280 Since 4.02.0
281
282
283 type location = {
284 filename : string ;
285 line_number : int ;
286 start_char : int ;
287 end_char : int ;
288 }
289
290
291 The type of location information found in backtraces. start_char and
292 end_char are positions relative to the beginning of the line.
293
294
295 Since 4.02
296
297
298 module Slot : sig end
299
300
301 Since 4.02.0
302
303
304
305
306 Raw backtrace slots
307 type raw_backtrace_slot
308
309
310 This type allows direct access to raw backtrace slots, without any con‐
311 version in an OCaml-usable data-structure. Being process-specific, they
312 must absolutely not be marshalled, and are unsafe to use for this rea‐
313 son (marshalling them may not fail, but un-marshalling and using the
314 result will result in undefined behavior).
315
316 Elements of this type can still be compared and hashed: when two ele‐
317 ments are equal, then they represent the same source location (the con‐
318 verse is not necessarily true in presence of inlining, for example).
319
320
321 Since 4.02.0
322
323
324
325 val raw_backtrace_length : raw_backtrace -> int
326
327
328 raw_backtrace_length bckt returns the number of slots in the backtrace
329 bckt .
330
331
332 Since 4.02
333
334
335
336 val get_raw_backtrace_slot : raw_backtrace -> int -> raw_backtrace_slot
337
338
339 get_raw_backtrace_slot bckt pos returns the slot in position pos in the
340 backtrace bckt .
341
342
343 Since 4.02
344
345
346
347 val convert_raw_backtrace_slot : raw_backtrace_slot -> backtrace_slot
348
349 Extracts the user-friendly backtrace_slot from a low-level raw_back‐
350 trace_slot .
351
352
353 Since 4.02
354
355
356
357 val get_raw_backtrace_next_slot : raw_backtrace_slot -> raw_back‐
358 trace_slot option
359
360
361 get_raw_backtrace_next_slot slot returns the next slot inlined, if any.
362
363 Sample code to iterate over all frames (inlined and non-inlined): (*
364 Iterate over inlined frames *) let rec iter_raw_backtrace_slot f slot =
365 f slot; match get_raw_backtrace_next_slot slot with | None -> () | Some
366 slot' -> iter_raw_backtrace_slot f slot' (* Iterate over stack frames
367 *) let iter_raw_backtrace f bt = for i = 0 to raw_backtrace_length bt -
368 1 do iter_raw_backtrace_slot f (get_raw_backtrace_slot bt i) done
369
370
371
372 Since 4.04.0
373
374
375
376
377 Exception slots
378 val exn_slot_id : exn -> int
379
380
381 Printexc.exn_slot_id returns an integer which uniquely identifies the
382 constructor used to create the exception value exn (in the current run‐
383 time).
384
385
386 Since 4.02.0
387
388
389
390 val exn_slot_name : exn -> string
391
392
393 Printexc.exn_slot_name exn returns the internal name of the constructor
394 used to create the exception value exn .
395
396
397 Since 4.02.0
398
399
400
401
402
403OCamldoc 2019-07-30 Printexc(3)