How to exit from java program




















In this Java tutorial, we will talk about the ways to terminate a Java Program with example. Normally, we use the return statement in the main method to exit the program. In this post, we will see how we can use the exit method to do the same.

The declaration for java. Halt is rarely used. Halt can be disabled by the SecurityManager class. In most circumstances, a program will terminate itself when it reaches the end, but in unusual circumstances, such as exceptions, a developer must know how to terminate a program.

Java provides several ways to terminate a program. These include the above mentioned topics. Developers must be cautious when using these termination methods.

With this article at OpenGenus, you must have the complete knowledge to terminate a program in Java using different techniques. Ways To Terminate Program in Java Terminating a program means to stop the execution of the program and more specifically, kill the process that was executing the program.

Syntax : System. Any other integer value when some error has occurred. Different values can denote different kinds of Errors. This program does not compile because there is a sysout statement after return statement. That mean compiler detects that last line of the program is never going to be executed with the error message " Unreachable code ". Exception in thread "main" java.

Error: Unresolved compilation problem: Unreachable code at com. If the condition is met then execute the logic and return "even number" and from else condition return "odd number" value. In this article, we've seen how to end the program in java using System. Because in your case the function is the main function of your application, return; will also terminate the application. But the return; in your example is useless, because the function will terminate directly after this return anyway The System.

It is allowed since compiler doesn't know calling System. The compiler will just give a warning - unnecessary return statement. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Terminating a Java Program Ask Question. Asked 7 years, 10 months ago. Active 5 months ago. Viewed k times. Improve this question.

Community Bot 1 1 1 silver badge. Cataclysm Cataclysm 6, 16 16 gold badges 66 66 silver badges bronze badges. Add a comment. Active Oldest Votes. If you try to put code after it, the compiler will complain about unreachable code, for example: public static void main String Improve this answer. Aleks G Aleks G



0コメント

  • 1000 / 1000