By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Processing your response...

Discuss This Question: 14 &nbspReplies

There was an error processing your information. Please try again later.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Hi Tom. This was a question asked to me in an interview. I am activly trying to find if there can be anything which can stop System.exit from running. I got that we can throw SecurityException using SecurityManager. But it throws an exception after giving desired output. So even that is not a valid answer of this question

Actually system.exit does when want to return a non-0 error code / want to exit progs from somewhere that isn't main()..... it also does abnormal termination or in javase api terminates the currently running jvm. As ur Q, there is a way to avoid by using Security Managerinstead of using syste.exit code.....if u write like this: System.exit(0); System.out.println ("any words");The 2nd line never be reached as prog exists before. These method throws an exception which, ultimately, is
caught by main(), which calls System.exit with the appropriate
code.

Again you didn't ask a question. And again you had a question in your title:
How can we bypass System.exit from executing?
But your title doesn't seem to have anything to do with what you wrote in the question area. Should we answer your title? Or should we address what you put into the question area which was a problem about changing the output. Why did you put the program code in the question area if you only wanted to know how to bypass System.exit? What is the purpose of showing us the program since you aren't asking about it?
Tom

Was that a typo where you wrote "Good Byye"? Maybe that's what confused me. It looked like you had some specific output that was different by adding an extra "y" from what was available in the code. -- Tom

Hi Tom.. Both the things are related. If you can tell me how can i bypass System.exit then by using it I can change the above program and I can print the expected output. Currently my program wont print "Good Bye" because of the use of System.exit

Ah. Okay, that makes more sense. Or at least it doesn't add a possible distraction.However, you should understand first that we don't answer interview (nor homework) questions. We can help guide the search for answers, usually by suggesting ideas or references that must be researched by the questioner. That ensures that future readers will also do their own research.Understood?Tom

I tried in this way using SecurityManagerpublic class t1{static{SecurityManager sm=new SecurityManager(){public void checkExit(int exitCode){System.out.println("Good Bye"); /* Another way is to throw SecurityException over here. Then the finally block will execute but an exception will be thrown after that*/}/*Overriding println method so that it will print false */};System.setSecurityManager(sm);}public static void main(String args[]){try{System.out.println("Welcome");System.exit(0);}finally{System.out.println("Good Bye");}}}

I tried the below thing using SecurityManagerI tried the below thing using SecurityManager
public class t1{
static{
SecurityManager sm=new SecurityManager(){
public void checkExit(int exitCode){
System.out.println("Good Bye"); /* Another way is to throw SecurityException over here. Then the finally block will execute but an exception will be thrown after that*/
}/*Overriding println method so that it will print false */
};
System.setSecurityManager(sm);
}
public static void main(String args[]){
try{
System.out.println("Welcome");
System.exit(0);
}
finally{
System.out.println("Good Bye");
}
}
}

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy

Processing your reply...

Ask a Question

Free Guide: Managing storage for virtual environments

Complete a brief survey to get a complimentary 70-page whitepaper featuring the best methods and solutions for your virtual environment, as well as hypervisor-specific management advice from TechTarget experts. Don’t miss out on this exclusive content!

To follow this tag...

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States.
Privacy