...
Code Block | ||
---|---|---|
| ||
class PasswordSecurityManager extends SecurityManager{ private boolean flagisExitAllowedFlag; public PasswordSecurityManager(){ super(); flagisExitAllowedFlag = false; } public boolean isExitAllowed(){ if(flag == true) return true; else return falseisExitAllowedFlag; } public void checkExit(int status) { if(!isExitAllowed()) throw new SecurityException(); } public void setExitAllowed(boolean f) { if(fisExitAllowedFlag == true) flag = true; else flag = false= f; } } public class InterceptExit { public static void main(String[] args) { PasswordSecurityManager secManager = new PasswordSecurityManager(); System.setSecurityManager(secManager); try { System.out.println("Regular code block"); System.exit(1); //abrupt exit call } catch (Throwable x) { if (x instanceof SecurityException) System.out.println("Intercepted System.exit()"); else x.printStackTrace(); } System.out.println("Executing code block..."); secManager.setExitAllowed(true); //permit exit System.out.println("Finished block, exiting..."); //exit finally } } |
...