How can we bypass System.exit from executing? Can anyone help me with the question

100 pts.
Tags:
Java
Re write the below program in such a way that it should print

How I can I make the finally block execute without changing anything in the main method? I tried using SecurityManager but it throws SecurityException after executing finally block Hi
Good Bye


NOTE: You cannot change the main method

Class Program{
Public static void main(String args[]){
Try{
System.out.println(“Hi”);
System.exit(0);
}
Finally{
System.out.println(“Good Bye”);
}} }

Software/Hardware used:
java

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
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

Discuss This Question: 14  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
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
  • TomLiotta
    Re write the below program...   Why in the world would we do that? Are you having a problem with it? If so, what problem are you having? Is there a part that you don't understand?   Tom
    125,585 pointsBadges:
    report
  • kinshu22
    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
    100 pointsBadges:
    report
  • Subhendu Sen
    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 Manager
    instead 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.

    29,350 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report
  • kinshu22
    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
    100 pointsBadges:
    report
  • kinshu22
    Sorry. That was typo.. Corrected it.. I want to execute finally bock even after System.exit.
    100 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report
  • carlosdl
    So, you have to "Re write the below program", but you can't modify the main method, and the only method the program has is the "main" one.  Is that right ?
    69,990 pointsBadges:
    report
  • kinshu22
    Yes. But you can add your own method or static block so that finally can run even after System.exit is called.
    100 pointsBadges:
    report
  • carlosdl
    Is it really a requirement that the finally block be executed ? How about doing something else but printing the desired text ? Also, can you post what you tried with SecurityManager ?
    69,990 pointsBadges:
    report
  • carlosdl
    I mentioned that because there is a Runtime method you can use to "intercept" the exit execution, from which you could do something before the VM actually goes down.
    69,990 pointsBadges:
    report
  • kinshu22
    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");}}}
    100 pointsBadges:
    report
  • kinshu22
    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"); } } }
    100 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

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

REGISTER or login:

Forgot Password?
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

Thanks! We'll email you when relevant content is added and updated.

Following