Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4a4b53dd167713a52816f4b5ec7ee1086804c153 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/

/*
 * Exception handling. Functionality is similar to C++ try/catch.
 * Usage example:
    Trap trap;
    if (set_trap(&trap)) {
        // Some code that can throw an exception by calling exception()
        ...

        clear_trap(&trap);
    }
    else {
        // Exception handling code
        if (trap.error == ...
        ...
    }
 * Only main thread is allowed to use exceptions.
 */

#ifndef D_exceptions
#define D_exceptions

#include <setjmp.h>
#include <framework/errors.h>

typedef struct Trap Trap;

struct Trap {
    int error;
    jmp_buf env;
    Trap * next;
};

#define set_trap(trap) (set_trap_a(trap), (trap)->error = setjmp((trap)->env), set_trap_b(trap))

extern int set_trap_a(Trap * trap);
extern int set_trap_b(Trap * trap);

extern void clear_trap(Trap * trap);

extern void exception(int error);
extern void str_exception(int error, const char * msg);
extern void str_fmt_exception(int error, const char * fmt, ...);

#endif /* D_exceptions */

Back to the top