Javatpoint Logo
Javatpoint Logo

How to Terminate a Program in Java?

Terminating an application in Java might also look like a straightforward challenge, however there are various techniques to gracefully terminate a given program or forcefully terminate it in case of surprising issues. In this section, we will discuss various ways to terminate a Java program and provide examples with comment strains to ensure clarity.

Using the System.exit() Method

The maximum direct manner to terminate a Java program is by using the System.Exit() method. The method permits us to specify an exit code that may be used to signify the purpose for termination. An go out code of zero normally signifies a hit termination, even as any non-zero cost shows an error or odd termination.

ExitExample.java

Output:

Program is about to exit.

In this example, this code absolutely prints a message after which exits with an exit code of zero. We can change the exit code to any other integer value if necessary.

Using return Statement:

In Java, we can also terminate a program by using the return statement. However, this method is handiest relevant inside methods, and it permits us to go out from a specific technique, now not the entire code. If used within the main() approach, it will successfully cease this program as well.

ReturnExample.java

Output:

This is before the return statement.

In this situation, the return announcement is used to go out, and as a result, the program terminates. Anything after the go back announcement will no longer be done.

Using Exceptions

Another manner to terminate a Java code is by way of throwing an unhandled exception. When an unhandled exception is thrown, it's going to propagate up the decision stack until it reaches the top-level errors handler that commonly terminates this code.

ExceptionExample.java

Output:

This is before the exception is thrown.
Exception in thread "main" java.lang.RuntimeException: This is an unhandled exception.
    at ExceptionExample.main(ExceptionExample.java:4)

In the above-mentioned example, an unhandled RuntimeException is thrown, and the code terminates with a display error message.

Using System.exit() with Non-Zero Exit Codes

In practice, we often use the System.exit() method with non-zero exit codes to suggest exclusive conditions of exit. For example, we would possibly use a non-zero exit code to signal that an error happened at some point of the code execution.

ExitCodeExample.java

Output:

Program executed effectively.

In this situation, the applySomeOperation() method simulates an error, and if an error occurs, this code exits with a non-zero exit code (1).

Using a Loop to Control Program Termination:

Sometimes, we may need to keep your application executing till a specific situation is met. We can use a loop to manipulate the program's termination. The program will maintain execution until condition for the loop is satisfied.

LoopExample.java

Output:

Running... Count: 0
Running... Count: 1
Running... Count: 2
Running... Count: 3
Running... Count: 4
Program completed successfully.

In Conclusion, terminating a Java code may be finished using several strategies, such as System.exit(), return statements, unhandled exceptions, and sign handlers. The method we pick out relies upon at the program's specific necessities. Always ensure that exit code gracefully, cope with mistakes efficaciously, and cope with thread termination if we are going for walks with a couple of threads. Proper code termination is crucial for maintaining the reliability and balance of Java code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA