17

I've done some research to know if there is an equivalent to PHP die in java

Sometimes I'm doing small tests and I really want to stop my code at specific line , and return doesnt stop the code like "die" in php

Is there a die quivalent in java?

0

4 Answers 4

28

Use System.exit(0); to exit the java code.

Keep a note this will stop the JVM instance which is currently running.

If you want to come out of a method use return or throw exception to showcase error condition.

Sign up to request clarification or add additional context in comments.

2 Comments

Afaik, you would have to give it an argument. I don't know if you left that out intentionally. For the OP, 0 for successful exit, anything else (integer) for error is what's generally done (iirc)
Yes, I figured that right now, it gave an error in eclipse without argument. but thats what I wanted, thank's alot
6

try:

System.out.println(message);
System.exit(0);

Comments

0

Disclaimer: I'm not that familiar with Java, I have a basic working knowledge of it, but haven't done a huge amount of work with it.

I suppose you could just throw() an exception and not catch it, that should halt execution. Of course if you have an exception handler for exceptions that aren't caught locally you might have problems.

Most IDEs let you set breakpoints where execution will stop and you'll be able to examine the state of variables, look at the call stack and so on. Both Eclipse and Netbeans support this. It might be a better option.

EDIT: There is also System.exit() which will halt execution. I still think breakpoints are a better option though.

Comments

0
<% if(true)return; %>  ok

4 Comments

This does not reply the OP question, maybe you do not know what die exactly do in PHP... And please, add at least some explanation to your answers.
return in JSP file ends JSP processing so it actually does the same as die in PHP... If you are in the JSP file of course.
Who said anything about JSP? Java is much bigger than that.
@JHH of course. So, could you please provide a unique answer for all java frameworks and corner cases? return works for Java Servlet Pages. PHP-FPM initiates a worker for every request, thus die() just flushes the socket as is and kills the worker. So do other servers like apache or nginx Java can have multiple ways of handling the requests (php also, but noone implements a web server inside a web server on php). Thus, if you are in a thread, you can try terminating your own thread. Other way is to throw some exception (DieException) and properly handle it till the entry (exit) point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.