I'm trying to make a simple Java wordcounting program.
When it reads Microsoft Office files, it first reads the text of the XML files (Microsoft Office files are actually bundles of zipped xml files!) and then reads them to a folder called "converted".
I want "converted" to be deleted right after the program ends, so I added a
new File("converted").deleteOnExit();
which does that well.
However, if the user of the program presses Ctrl+C in the command prompt, then the program will exit early, and the folder will not be deleted.
I would like to know if there's a way to throw an exception if a program is exited. It seems unlikely, because a forced exit of a program will probably stop any code, but I was wondering if this is possible. Then, I'll be able to handle the exception and exit the program correctly, so that the directory will be deleted. I mean, I can add this:
catch(ExitException e) { // if the exception is called "ExitException"
System.err.format("Program ended unexpectedly.%n");
System.exit(-1); // this line so that the folder can delete
}
(The way I understand it, the folder is only deleted if a System.exit()
is called. Correct me if I'm wrong.)
Thanks.