Skip to content

Instantly share code, notes, and snippets.

@laparca
Last active August 29, 2015 14:16
Show Gist options
  • Save laparca/12b5a6220134041bbef7 to your computer and use it in GitHub Desktop.
Save laparca/12b5a6220134041bbef7 to your computer and use it in GitHub Desktop.
Error handling
if (my_function() == ERROR) {
/* do error management */
}
match my_function() {
IOError => /* do io error management */,
LogicError => /* do logic error management */,
_ => /* Other errors management */
}
typedef enum {
RETRY, /* Si hay error, reintentamos */
CANCEL, /* Si hay error, cancelamos la operacion y devolvemos el error */
CONTINUE, /* Si hay error, continuamos el resto de la operacion */
SKIP, /* Si hay error, salimos devolviendo que ha ido todo bien */
} err_action_t;
/**
* Define como sera nuestra funcion de tratamiento de errores
*/
typedef err_action_t (*err_callback_t)(void* err_info);
/**
* Funcion a la que llamaremos y que puede o no dar error.
* @param callback Funcion para informar del error y tratarlo
* @return Codigo de resutlado
*/
int a_large_and_very_complex_low_level_function(err_callback_t callback);
err_action_t my_function_err_callback(void* err_info) {
/* En este nivel si sabemos como mostrar informacion */
switch(showError("There was an error", BUTTON_RETRY | BUTTON_CANCEL)) {
case BUTTON_RETRY:
return RETRY;
case BUTTON_CANCEL:
return CANCEL;
}
return CANCEL;
}
/* Nuestra funcion de alto nivel que si puede mostrar o no informacion a usuario */
int my_function() {
if (a_large_and_very_complex_low_level_function(my_function_err_callback) == ERROR) {
/* Lo que tengamos que hacer en este caso */
}
return OK;
}
typedef enum {
RETRY, /* Si hay error, reintentamos */
CANCEL, /* Si hay error, cancelamos la operacion y devolvemos el error */
CONTINUE, /* Si hay error, continuamos el resto de la operacion */
SKIP, /* Si hay error, salimos devolviendo que ha ido todo bien */
} err_action_t;
err_action_t default_err_action() { return CANCEL; }
/**
* Funcion a la que llamaremos y que puede o no dar error.
* @param Functor Funcion para informar del error y tratarlo. Si no se indica
* se cancelara la cancelara y devolvera error.
* @return Codigo de resutlado
*/
template<typename Functor>
int a_large_and_very_complex_low_level_function(Functor functor = default_err_action);
/* Nuestra funcion de alto nivel que si puede mostrar o no informacion a usuario */
int my_function() {
int result;
result = a_large_and_very_complex_low_level_function([]() {
/* En este nivel si sabemos como mostrar informacion */
switch(showError("There was an error", BUTTON_RETRY | BUTTON_CANCEL)) {
case BUTTON_RETRY:
return RETRY;
case BUTTON_CANCEL:
return CANCEL;
}
return CANCEL;
});
if (result == ERROR) {
/* Lo que tengamos que hacer en este caso */
}
return OK;
}
// From rust manual
// Read a line from standar input and if it isn't OK
// it panic with "Failed to read line".
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
// Try to read a line from standar input. if it isn't
// ok it will return "Alternative text".
let input = io::stdin().read_line()
.ok()
.unwrap_or("Alternative text");
#include <setjmp.h>
jmp_buf app_status;
void my_function() {
longjmp(app_status, 1);
}
int main(void) {
switch(setjmp(app_status))
{
case 0:
my_function();
break;
case 1:
/* my_function fails and we have to fix the error */
break;
}
return 0;
}
#include <signal.h>
void sigsegv(int signum) {
/* do somthing with the SIGSEGV signal */
}
int main(void) {
char *ptr = (char *)0;
signal(SIGSEGV, sigsegv);
/* force the signal */
*ptr = 'a';
return 0;
}
try {
my_function();
}
catch(std::exception& e) {
/* do error management */
}
int main(void) {
int i;
__try {
i = 1/0;
}
__except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
/* do division by 0 management */
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment