Java 相当于 php 死了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10812061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:41:31  来源:igfitidea点击:

Java equivalent to php die

javaphpjakarta-ee

提问by Lamis

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

我已经做了一些研究,想知道在 Java 中是否有相当于 PHP die 的东西

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

有时我在做一些小测试,我真的想在特定行停止我的代码,并且 return 不会停止像 php 中的“死”这样的代码

Is there a die quivalent in java?

java中有一个等效的模具吗?

回答by mprabhat

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

使用System.exit(0);退出Java代码。

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

请注意这将停止当前正在运行的 JVM 实例。

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

如果你想从一个方法中出来,使用 return 或 throw 异常来展示错误情况。

回答by Euclides Mulémbwè

try:

尝试:

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

回答by Anatoly Vandraren

<% if(true)return; %>  ok

回答by GordonM

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.

免责声明:我对 Java 不太熟悉,我对它有基本的工作知识,但还没有做过大量的工作。

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.

我想你可以只 throw() 一个异常而不捕捉它,这应该停止执行。当然,如果您有一个用于本地未捕获的异常的异常处理程序,您可能会遇到问题。

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.

大多数 IDE 允许您设置执行将停止的断点,您将能够检查变量的状态,查看调用堆栈等。Eclipse 和 Netbeans 都支持这一点。这可能是一个更好的选择。

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

编辑:还有 System.exit() 将停止执行。我仍然认为断点是更好的选择。