0

I am creating an application where files are kept open so they can be easily read.

What I am trying to figure out is how to execute a block of code after the application ends even if an error is raised?

It's currently causing resource leaks so I need to handle the loose files.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
mateos
  • 1,405
  • 1
  • 17
  • 26
  • 2
    OK, the most important thing is: what does "program crashes" means? Do you mean "process went completely insane, it has to be killed" or "error occured, I don't know how to fix that, so I'm shutting down on my own"? First case is similiar to your physical machine running out of power, second is basically unhandled exception. You can install some hooks for second case, but there are some situations that are impossible to handle - power outage for example. – Filip Malczak Sep 06 '15 at 09:52
  • 1
    This is different question, but may be somehow helpful if you're asking about the latter of cases from previous comment: http://stackoverflow.com/questions/24312567/java-shutdown-hooks – Filip Malczak Sep 06 '15 at 09:54
  • @FilipMalczak Error Occured is what i mean – mateos Sep 06 '15 at 09:57
  • 1
    Then you need shutdown hooks - see link above. Also, IoC frameworks (IoC - Inversion of Control, some of the parts of that are DI - dependency injection - and framework structure) often provide object lifecycle handling, with support for non-successful program termination. See Spring for example. – Filip Malczak Sep 06 '15 at 16:02
  • http://stackoverflow.com/questions/19639319/java-shutdown-hook – kervin Sep 08 '15 at 11:25

0 Answers0