How to ensure a piece of code is run before exiting a java application

后端 未结 4 2029
庸人自扰
庸人自扰 2021-01-13 00:42

I\'m using a licensed API which has a method to acquire/release a license object from a license server that has a finite number of licenses. At the beginning of my applicati

4条回答
  •  失恋的感觉
    2021-01-13 01:22

    If the program is terminated through a crash of the JVM, you can't rely on anything being called.

    If the program is terminated through an exception that doesn't involve the JVM you should be able to wrap everything in a try/catch/finally block. Any code in the finally block would be guaranteed to run before your code exits.

提交回复
热议问题