SDL 3.0
SDL_error.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryError
24 *
25 * Simple error message routines for SDL.
26 */
27
28#ifndef SDL_error_h_
29#define SDL_error_h_
30
31#include <SDL3/SDL_stdinc.h>
32
33#include <SDL3/SDL_begin_code.h>
34/* Set up for C function definitions, even when using C++ */
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/* Public functions */
40
41
42/**
43 * Set the SDL error message for the current thread.
44 *
45 * Calling this function will replace any previous error message that was set.
46 *
47 * This function always returns -1, since SDL frequently uses -1 to signify an
48 * failing result, leading to this idiom:
49 *
50 * ```c
51 * if (error_code) {
52 * return SDL_SetError("This operation has failed: %d", error_code);
53 * }
54 * ```
55 *
56 * \param fmt a printf()-style message format string.
57 * \param ... additional parameters matching % tokens in the `fmt` string, if
58 * any.
59 * \returns -1.
60 *
61 * \since This function is available since SDL 3.0.0.
62 *
63 * \sa SDL_ClearError
64 * \sa SDL_GetError
65 */
66extern SDL_DECLSPEC int SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
67
68/**
69 * Set an error indicating that memory allocation failed.
70 *
71 * This function does not do any memory allocation.
72 *
73 * \returns -1.
74 *
75 * \since This function is available since SDL 3.0.0.
76 */
77extern SDL_DECLSPEC int SDLCALL SDL_OutOfMemory(void);
78
79/**
80 * Retrieve a message about the last error that occurred on the current
81 * thread.
82 *
83 * It is possible for multiple errors to occur before calling SDL_GetError().
84 * Only the last error is returned.
85 *
86 * The message is only applicable when an SDL function has signaled an error.
87 * You must check the return values of SDL function calls to determine when to
88 * appropriately call SDL_GetError(). You should *not* use the results of
89 * SDL_GetError() to decide if an error has occurred! Sometimes SDL will set
90 * an error string even when reporting success.
91 *
92 * SDL will *not* clear the error string for successful API calls. You *must*
93 * check return values for failure cases before you can assume the error
94 * string applies.
95 *
96 * Error strings are set per-thread, so an error set in a different thread
97 * will not interfere with the current thread's operation.
98 *
99 * The returned value is a thread-local string which will remain valid until
100 * the current thread's error string is changed. The caller should make a copy
101 * if the value is needed after the next SDL API call.
102 *
103 * \returns a message with information about the specific error that occurred,
104 * or an empty string if there hasn't been an error message set since
105 * the last call to SDL_ClearError().
106 *
107 * \since This function is available since SDL 3.0.0.
108 *
109 * \sa SDL_ClearError
110 * \sa SDL_SetError
111 */
112extern SDL_DECLSPEC const char * SDLCALL SDL_GetError(void);
113
114/**
115 * Clear any previous error message for this thread.
116 *
117 * \returns 0.
118 *
119 * \since This function is available since SDL 3.0.0.
120 *
121 * \sa SDL_GetError
122 * \sa SDL_SetError
123 */
124extern SDL_DECLSPEC int SDLCALL SDL_ClearError(void);
125
126/**
127 * \name Internal error functions
128 *
129 * \internal
130 * Private error reporting function - used internally.
131 */
132/* @{ */
133#define SDL_Unsupported() SDL_SetError("That operation is not supported")
134#define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
135/* @} *//* Internal error functions */
136
137/* Ends C function definitions when using C++ */
138#ifdef __cplusplus
139}
140#endif
141#include <SDL3/SDL_close_code.h>
142
143#endif /* SDL_error_h_ */
int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
int SDL_ClearError(void)
const char * SDL_GetError(void)
int SDL_OutOfMemory(void)
#define SDL_PRINTF_FORMAT_STRING
Definition SDL_stdinc.h:444
#define SDL_PRINTF_VARARG_FUNC(fmtargnumber)
Definition SDL_stdinc.h:455