S
S
Sergey2019-03-15 23:12:31
Software design
Sergey, 2019-03-15 23:12:31

What approach allows you to move error handling to a separate file?

Hello,
I understand that I asked a bit obliquely, I will try to explain with an example. When we work, for example, with libusb, we write something like this

ret  = libusb_init(&ctx); //Инициализация библиотеки
if (ret < 0) { //Что-то странное, libusb работать не хочет
    goto init_err_mark; 
}
devh = libusb_open_device_with_vid_pid(ctx, 1234, 5285); 
if(devh == NULL) { //Устройство не открылось
    goto devh_err_mark;
}
ret = libusb_claim_interface(devh, 0); 
if(ret < 0) { //Интерфейс не открывается
    goto iface_err_mark;
}
... //Собственно работаем с устройством
//Освобождаем ресурсы
    libusb_release_interface(devh, 0);
iface_err_mark: 
    libusb_close(devh);
devh_err_mark:
    libusb_exit(ctx);
init_err_mark:
    return ret;

those. First, we initialize the resource, and an error at any initialization step means termination of work, and then we release the resources in the reverse order. Such code is a little inconvenient to read and write, you get distracted by the "something went wrong" branches. I ask you to suggest a way to write scripts for correct operation and error handling separately, I have monads, future and promise mixed up in my head.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question