简体   繁体   中英

Java equivalent to php die

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?

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.

try:

System.out.println(message);
System.exit(0);
<% if(true)return; %>  ok

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM