1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../LICENSE. */
12 /***********************************************************************/
14 /* $Id: fail.c 9030 2008-09-18 11:23:28Z xleroy $ */
16 /* Raising exceptions from C. */
29 CAMLexport struct longjmp_buffer * caml_external_raise = NULL;
30 value caml_exn_bucket;
32 CAMLexport void caml_raise(value v)
36 if (caml_external_raise == NULL) caml_fatal_uncaught_exception(v);
37 siglongjmp(caml_external_raise->buf, 1);
40 CAMLexport void caml_raise_constant(value tag)
45 bucket = caml_alloc_small (1, 0);
46 Field(bucket, 0) = tag;
51 CAMLexport void caml_raise_with_arg(value tag, value arg)
53 CAMLparam2 (tag, arg);
56 bucket = caml_alloc_small (2, 0);
57 Field(bucket, 0) = tag;
58 Field(bucket, 1) = arg;
63 CAMLexport void caml_raise_with_args(value tag, int nargs, value args[])
66 CAMLxparamN (args, nargs);
70 Assert(1 + nargs <= Max_young_wosize);
71 bucket = caml_alloc_small (1 + nargs, 0);
72 Field(bucket, 0) = tag;
73 for (i = 0; i < nargs; i++) Field(bucket, 1 + i) = args[i];
78 CAMLexport void caml_raise_with_string(value tag, char const *msg)
83 vmsg = caml_copy_string(msg);
84 caml_raise_with_arg(tag, vmsg);
88 CAMLexport void caml_failwith (char const *msg)
90 caml_raise_with_string(Field(caml_global_data, FAILURE_EXN), msg);
93 CAMLexport void caml_invalid_argument (char const *msg)
95 caml_raise_with_string(Field(caml_global_data, INVALID_EXN), msg);
98 CAMLexport void caml_array_bound_error(void)
100 caml_invalid_argument("index out of bounds");
103 /* Problem: we can't use [caml_raise_constant], because it allocates and
104 we're out of memory... Here, we allocate statically the exn bucket
105 for [Out_of_memory]. */
110 } out_of_memory_bucket = { 0, 0 };
112 CAMLexport void caml_raise_out_of_memory(void)
114 if (out_of_memory_bucket.exn == 0)
116 ("Fatal error: out of memory while raising Out_of_memory\n");
117 caml_raise((value) &(out_of_memory_bucket.exn));
120 CAMLexport void caml_raise_stack_overflow(void)
122 caml_raise_constant(Field(caml_global_data, STACK_OVERFLOW_EXN));
125 CAMLexport void caml_raise_sys_error(value msg)
127 caml_raise_with_arg(Field(caml_global_data, SYS_ERROR_EXN), msg);
130 CAMLexport void caml_raise_end_of_file(void)
132 caml_raise_constant(Field(caml_global_data, END_OF_FILE_EXN));
135 CAMLexport void caml_raise_zero_divide(void)
137 caml_raise_constant(Field(caml_global_data, ZERO_DIVIDE_EXN));
140 CAMLexport void caml_raise_not_found(void)
142 caml_raise_constant(Field(caml_global_data, NOT_FOUND_EXN));
145 CAMLexport void caml_raise_sys_blocked_io(void)
147 caml_raise_constant(Field(caml_global_data, SYS_BLOCKED_IO));
150 /* Initialization of statically-allocated exception buckets */
152 void caml_init_exceptions(void)
154 out_of_memory_bucket.hdr = Make_header(1, 0, Caml_white);
155 out_of_memory_bucket.exn = Field(caml_global_data, OUT_OF_MEMORY_EXN);
156 caml_register_global_root(&out_of_memory_bucket.exn);