1Stdlib.Printexc(3)               OCaml library              Stdlib.Printexc(3)
2
3
4

NAME

6       Stdlib.Printexc - no description
7

Module

9       Module   Stdlib.Printexc
10

Documentation

12       Module Printexc
13        : (module Stdlib__Printexc)
14
15
16
17
18
19
20
21       type t = exn = ..
22
23
24       The type of exception values.
25
26
27
28       val to_string : exn -> string
29
30
31       Printexc.to_string e returns a string representation of the exception e
32       .
33
34
35
36       val to_string_default : exn -> string
37
38
39       Printexc.to_string_default e returns a string representation of the ex‐
40       ception e , ignoring all registered exception printers.
41
42
43       Since 4.09
44
45
46
47       val print : ('a -> 'b) -> 'a -> 'b
48
49
50       Printexc.print  fn  x  applies  fn to x and returns the result.  If the
51       evaluation of fn x raises any exception, the name of the  exception  is
52       printed  on  standard  error output, and the exception is raised again.
53       The typical use is to catch and report exceptions that escape  a  func‐
54       tion application.
55
56
57
58       val catch : ('a -> 'b) -> 'a -> 'b
59
60       Deprecated.  This function is no longer needed.
61
62
63
64       Printexc.catch  fn x is similar to Printexc.print , but aborts the pro‐
65       gram with exit code 2 after  printing  the  uncaught  exception.   This
66       function  is  deprecated:  the  runtime system is now able to print un‐
67       caught exceptions as precisely as Printexc.catch does.  Moreover, call‐
68       ing  Printexc.catch makes it harder to track the location of the excep‐
69       tion using the debugger or the stack backtrace facility.   So,  do  not
70       use Printexc.catch in new code.
71
72
73
74       val print_backtrace : out_channel -> unit
75
76
77       Printexc.print_backtrace oc prints an exception backtrace on the output
78       channel oc .  The backtrace  lists  the  program  locations  where  the
79       most-recently  raised  exception was raised and where it was propagated
80       through function calls.
81
82       If the call is not inside an exception handler, the returned  backtrace
83       is  unspecified. If the call is after some exception-catching code (be‐
84       fore in the handler, or in a when-guard during the matching of the  ex‐
85       ception  handler),  the  backtrace  may correspond to a later exception
86       than the handled one.
87
88
89       Since 3.11.0
90
91
92
93       val get_backtrace : unit -> string
94
95
96       Printexc.get_backtrace () returns a string containing the  same  excep‐
97       tion backtrace that Printexc.print_backtrace would print. Same restric‐
98       tion usage than Printexc.print_backtrace .
99
100
101       Since 3.11.0
102
103
104
105       val record_backtrace : bool -> unit
106
107
108       Printexc.record_backtrace b turns recording of exception backtraces  on
109       (if  b  = true ) or off (if b = false ).  Initially, backtraces are not
110       recorded, unless the b flag is given to the program through the  OCAML‐
111       RUNPARAM variable.
112
113
114       Since 3.11.0
115
116
117
118       val backtrace_status : unit -> bool
119
120
121       Printexc.backtrace_status()  returns  true  if exception backtraces are
122       currently recorded, false if not.
123
124
125       Since 3.11.0
126
127
128
129       val register_printer : (exn -> string option) -> unit
130
131
132       Printexc.register_printer fn registers fn as an exception printer.  The
133       printer  should  return  None or raise an exception if it does not know
134       how to convert the passed exception, and Some
135           s with s the resulting string if it can convert the  passed  excep‐
136       tion. Exceptions raised by the printer are ignored.
137
138       When  converting  an  exception into a string, the printers will be in‐
139       voked in the reverse order of their registrations, until a printer  re‐
140       turns a Some s value (if no such printer exists, the runtime will use a
141       generic printer).
142
143       When using this mechanism, one should be aware that an exception  back‐
144       trace  is attached to the thread that saw it raised, rather than to the
145       exception itself. Practically, it means that the  code  related  to  fn
146       should  not  use the backtrace if it has itself raised an exception be‐
147       fore.
148
149
150       Since 3.11.2
151
152
153
154       val use_printers : exn -> string option
155
156
157       Printexc.use_printers e returns None if there are no registered  print‐
158       ers and Some s with else as the resulting string otherwise.
159
160
161       Since 4.09
162
163
164
165
166   Raw backtraces
167       type raw_backtrace
168
169
170       The  type raw_backtrace stores a backtrace in a low-level format, which
171       can be converted to usable form using raw_backtrace_entries  and  back‐
172       trace_slots_of_raw_entry below.
173
174       Converting  backtraces to backtrace_slot s is slower than capturing the
175       backtraces. If an application processes many backtraces, it can be use‐
176       ful to use raw_backtrace to avoid or delay conversion.
177
178       Raw  backtraces  cannot  be  marshalled.  If  you need marshalling, you
179       should use the array returned by the backtrace_slots  function  of  the
180       next section.
181
182
183       Since 4.01.0
184
185
186       type raw_backtrace_entry = private int
187
188
189       A raw_backtrace_entry is an element of a raw_backtrace .
190
191       Each  raw_backtrace_entry is an opaque integer, whose value is not sta‐
192       ble between different programs, or even between different runs  of  the
193       same binary.
194
195       A  raw_backtrace_entry  can  be  converted to a usable form using back‐
196       trace_slots_of_raw_entry below. Note that, due to  inlining,  a  single
197       raw_backtrace_entry may convert to several backtrace_slot s.  Since the
198       values of a raw_backtrace_entry are not stable,  they  cannot  be  mar‐
199       shalled.  If  they  are to be converted, the conversion must be done by
200       the process that generated them.
201
202       Again due to inlining, there may be multiple distinct raw_backtrace_en‐
203       try  values  that  convert  to  equal backtrace_slot s. However, if two
204       raw_backtrace_entry s are equal as integers, then  they  represent  the
205       same backtrace_slot s.
206
207
208       Since 4.12.0
209
210
211
212       val raw_backtrace_entries : raw_backtrace -> raw_backtrace_entry array
213
214       Since 4.12.0
215
216
217
218       val get_raw_backtrace : unit -> raw_backtrace
219
220
221       Printexc.get_raw_backtrace () returns the same exception backtrace that
222       Printexc.print_backtrace would print, but in a  raw  format.  Same  re‐
223       striction usage than Printexc.print_backtrace .
224
225
226       Since 4.01.0
227
228
229
230       val print_raw_backtrace : out_channel -> raw_backtrace -> unit
231
232       Print a raw backtrace in the same format Printexc.print_backtrace uses.
233
234
235       Since 4.01.0
236
237
238
239       val raw_backtrace_to_string : raw_backtrace -> string
240
241       Return  a  string  from  a  raw  backtrace,  in  the same format Print‐
242       exc.get_backtrace uses.
243
244
245       Since 4.01.0
246
247
248
249       val raise_with_backtrace : exn -> raw_backtrace -> 'a
250
251       Reraise the exception using the given raw_backtrace for the  origin  of
252       the exception
253
254
255       Since 4.05.0
256
257
258
259
260   Current call stack
261       val get_callstack : int -> raw_backtrace
262
263
264       Printexc.get_callstack  n  returns a description of the top of the call
265       stack on the current program point (for the current  thread),  with  at
266       most  n  entries.  (Note: this function is not related to exceptions at
267       all, despite being part of the Printexc module.)
268
269
270       Since 4.01.0
271
272
273
274
275   Uncaught exceptions
276       val default_uncaught_exception_handler : exn -> raw_backtrace -> unit
277
278
279       Printexc.default_uncaught_exception_handler prints  the  exception  and
280       backtrace on standard error output.
281
282
283       Since 4.11
284
285
286
287       val  set_uncaught_exception_handler : (exn -> raw_backtrace -> unit) ->
288       unit
289
290
291       Printexc.set_uncaught_exception_handler fn registers fn as the  handler
292       for  uncaught  exceptions.  The default handler is Printexc.default_un‐
293       caught_exception_handler .
294
295       Note that when fn is called all the functions registered  with  at_exit
296       have already been called. Because of this you must make sure any output
297       channel fn writes on is flushed.
298
299       Also note that exceptions  raised  by  user  code  in  the  interactive
300       toplevel  are  not  passed  to  this function as they are caught by the
301       toplevel itself.
302
303       If fn raises an exception, both the exceptions passed to fn and  raised
304       by fn will be printed with their respective backtrace.
305
306
307       Since 4.02.0
308
309
310
311
312   Manipulation of backtrace information
313       These  functions  are used to traverse the slots of a raw backtrace and
314       extract information from them in a programmer-friendly format.
315
316       type backtrace_slot
317
318
319       The abstract type backtrace_slot represents a single slot  of  a  back‐
320       trace.
321
322
323       Since 4.02
324
325
326
327       val backtrace_slots : raw_backtrace -> backtrace_slot array option
328
329       Returns  the  slots of a raw backtrace, or None if none of them contain
330       useful information.
331
332       In the return array, the slot at index 0 corresponds to the most recent
333       function call, raise, or primitive get_backtrace call in the trace.
334
335       Some possible reasons for returning None are as follow:
336
337       -none  of  the slots in the trace come from modules compiled with debug
338       information ( -g )
339
340       -the program is a bytecode program that has not been linked with  debug
341       information enabled ( ocamlc -g )
342
343
344
345       Since 4.02.0
346
347
348
349       val   backtrace_slots_of_raw_entry   :   raw_backtrace_entry  ->  back‐
350       trace_slot array option
351
352       Returns the slots of a single raw backtrace entry, or None if this  en‐
353       try lacks debug information.
354
355       Slots  are  returned in the same order as backtrace_slots : the slot at
356       index 0 is the most recent call, raise, or  primitive,  and  subsequent
357       slots represent callers.
358
359
360       Since 4.12
361
362
363       type location = {
364        filename : string ;
365        line_number : int ;
366        start_char : int ;
367        end_char : int ;
368        }
369
370
371       The  type  of location information found in backtraces.  start_char and
372       end_char are positions relative to the beginning of the line.
373
374
375       Since 4.02
376
377
378       module Slot : sig end
379
380
381       Since 4.02.0
382
383
384
385
386   Raw backtrace slots
387       type raw_backtrace_slot
388
389
390       This type is used to iterate over the slots of a raw_backtrace  .   For
391       most purposes, backtrace_slots_of_raw_entry is easier to use.
392
393       Like raw_backtrace_entry , values of this type are process-specific and
394       must absolutely not be marshalled, and are unsafe to use for this  rea‐
395       son  (marshalling  them  may not fail, but un-marshalling and using the
396       result will result in undefined behavior).
397
398       Elements of this type can still be compared and hashed: when  two  ele‐
399       ments are equal, then they represent the same source location (the con‐
400       verse is not necessarily true in presence of inlining, for example).
401
402
403       Since 4.02.0
404
405
406
407       val raw_backtrace_length : raw_backtrace -> int
408
409
410       raw_backtrace_length bckt returns the number of slots in the  backtrace
411       bckt .
412
413
414       Since 4.02
415
416
417
418       val get_raw_backtrace_slot : raw_backtrace -> int -> raw_backtrace_slot
419
420
421       get_raw_backtrace_slot bckt pos returns the slot in position pos in the
422       backtrace bckt .
423
424
425       Since 4.02
426
427
428
429       val convert_raw_backtrace_slot : raw_backtrace_slot -> backtrace_slot
430
431       Extracts the user-friendly backtrace_slot from  a  low-level  raw_back‐
432       trace_slot .
433
434
435       Since 4.02
436
437
438
439       val   get_raw_backtrace_next_slot  :  raw_backtrace_slot  ->  raw_back‐
440       trace_slot option
441
442
443       get_raw_backtrace_next_slot slot returns the next slot inlined, if any.
444
445       Sample code to iterate over all frames (inlined and non-inlined):
446             (* Iterate over inlined frames *)
447             let rec iter_raw_backtrace_slot f slot =
448               f slot;
449               match get_raw_backtrace_next_slot slot with
450               | None -> ()
451               | Some slot' -> iter_raw_backtrace_slot f slot'
452
453             (* Iterate over stack frames *)
454             let iter_raw_backtrace f bt =
455               for i = 0 to raw_backtrace_length bt - 1 do
456                 iter_raw_backtrace_slot f (get_raw_backtrace_slot bt i)
457               done
458
459
460
461       Since 4.04.0
462
463
464
465
466   Exception slots
467       val exn_slot_id : exn -> int
468
469
470       Printexc.exn_slot_id returns an integer which uniquely  identifies  the
471       constructor used to create the exception value exn (in the current run‐
472       time).
473
474
475       Since 4.02.0
476
477
478
479       val exn_slot_name : exn -> string
480
481
482       Printexc.exn_slot_name exn returns the internal name of the constructor
483       used to create the exception value exn .
484
485
486       Since 4.02.0
487
488
489
490
491
492OCamldoc                          2023-07-20                Stdlib.Printexc(3)
Impressum