How to run code after calling Sytem.exit (0) in finally block

I have three classes: alpha, beta, gamma and each of the three classes has a main method.

Both alpha and beta classes have a try...catch...finally block inside their main method, for example:

 public class alpha{ public static void main(String[] args){ try{ Do something; }catch(Exception ex){ ex.printStackTrace(); } finally{ System.exit(0); } } } public class beta{ public static void main(String[] args){ try{ Do something; }catch(Exception ex){ ex.printStackTrace(); } finally{ System.exit(0); } } } 

Now, in the gamma class, I call the main alpha and beta classes to run continuously, as shown below

 public gamma{ public static void main(String[] args) { try { alpha.main(arg); beta.main(arg1); } catch (Exception e) { e.printStackTrace(); } } 

The problem is that beta.main(arg1) code is never reached due to System.exit(0) inside the finally alpha block class. Since alpha and beta are standalone applications, when they are run separately, they must stop servicing at the end of the program. So, now this is a way to reach the beta.main(arg1) without significantly changing the actual functionality of the alpha and beta classes.

Please let me know if you need more information. Thanks in advance...

+6
source share
4 answers

In this case, you can use the disconnect hook:

 public class Gamma{ public static void main(String[] args) { try { Thread hook = new Thread() { public void run() { Beta.main(args); } }; hook.setDaemon(true); Runtime.getRuntime().addShutdownHook(hook); Alpha.main(args); } catch (Exception e) { e.printStackTrace(); } } } 
+4
source

(Ideally, nothing that is part of the public API of the module should ever do anything that calls exit , and the main method of the class should just be a little padding that calls something else, which does the real work before creating the right one exit code.)

However, if you want to prevent System.exit , you can register a SecurityManager that translates calls on System.exit to SecurityException or Error s.

System.exit :

throws

SecurityException - if there is a security manager, and its checkExit method checkExit not allow exit with the specified status.

Sort of

 System.setSecurityManager(new SecurityManager() { @Override public void checkExit(int exitCode) throws SecurityException { throw new SecurityException("stop that"); } }); 

Then a method that calls the main methods can simply catch and suppress this SecurityException . You can make it more reliable by creating your own ExitCalledError and throwing it away and only suppressing it instead.

I find this very useful in order to prevent runners with single testing from reporting the success error when the test runner exit edited with test code with a zero exit code.

+2
source

Indeed, the only solution is to get rid of the call to System.exit (). This is why System.exit () is evil. A good way to replace them is with an exception - you can add an exception handler to the system (look at adding them to ThreadGroups to add one for each exception path), and then decide what you want to do.

+1
source

System.exit(0) terminates the currently running Java virtual machine. It closes all applications in the virtual machine, not just the application that calls System.exit(0) . You need to think about an alternative for your functionality. Here is a link to it. Using System.exit

0
source

Source: https://habr.com/ru/post/905114/


All Articles