Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an application-wide exception handler make sense?

Long story short, I have a substantial Python application that, among other things, does outcalls to "losetup", "mount", etc. on Linux. Essentially consuming system resources that must be released when complete.

If my application crashes, I want to ensure these system resources are properly released.

Does it make sense to do something like the following?

def main():
    # TODO: main application entry point
    pass

def cleanup():
    # TODO: release system resources here
    pass

if __name__ == "__main__":
    try:
        main()
    except:
        cleanup()
        raise

Is this something that is typically done? Is there a better way? Perhaps the destructor in a singleton class?

like image 368
EmmEff Avatar asked Sep 18 '08 18:09

EmmEff


People also ask

Is global exception handling good practice?

But yes global exception handler are sometime important to use where our application scope is too big and on every exception we just need to show user a simple error message. It will save your development time and also reduce code.

What is difference between @ControllerAdvice and Exceptionhandler?

@ControllerAdvice is not specific to the exception handling , its also used for handling property, validation or formatter bindings at the global level. @ControllerAdvice in the context of exception handling is just another way of doing exception handling at a global level using @Exceptionhandler annotation.

What is the purpose of exception handler?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.

What should the system do to avoid error handling?

Do: Work on exceptions that directly impact the user experience. Don't: Build KPIs around reducing errors or exceptions by a certain percentage or targeting a specific number. Do: Prioritize errors related to your user's personal identifiable information, billing cycle, and functions that could corrupt your database.


1 Answers

I like top-level exception handlers in general (regardless of language). They're a great place to cleanup resources that may not be immediately related to resources consumed inside the method that throws the exception.

It's also a fantastic place to log those exceptions if you have such a framework in place. Top-level handlers will catch those bizarre exceptions you didn't plan on and let you correct them in the future, otherwise, you may never know about them at all.

Just be careful that your top-level handler doesn't throw exceptions!

like image 118
Bob King Avatar answered Nov 15 '22 15:11

Bob King