Java:我在Java代码中有try、catch和finally,我希望在try之后或catch块finally不执行
我有以下代码Java:我在Java代码中有try、catch和finally,我希望在try之后或catch块finally不执行,java,exception-handling,Java,Exception Handling,我有以下代码 public class TEST { public static void main(String arg[]){ try { System.out.println("execute try"); //what should I write hear that finally does not run. } catch (Exception e){ System.out.println(e); }
public class TEST
{
public static void main(String arg[]){
try
{
System.out.println("execute try");
//what should I write hear that finally does not run.
}
catch (Exception e){
System.out.println(e);
}
finally{
System.out.println("execute finally");
}
}
}
我应该在最终不运行的try或catch块中写什么。有什么想法吗?您需要通过调用exit as关闭JVM:
System.exit(0);
从:
如果JVM在执行try或catch代码时退出,那么finally块可能不会执行。同样,如果执行try或catch代码的线程被中断或终止,那么即使整个应用程序继续运行,finally块也可能不会执行
您需要通过调用exit作为关闭JVM:
从:
如果JVM在执行try或catch代码时退出,那么finally块可能不会执行。同样,如果执行try或catch代码的线程被中断或终止,那么即使整个应用程序继续运行,finally块也可能不会执行
如果您不想让某些东西在finally块中运行,请不要将其放入finally。Finally总是运行良好,除了其他人提到的一些情况。如果您不想在Finally块中运行某些内容,请不要将其放入Finally。Finally总是运行得很好,除了其他人提到的一些情况。将代码放入Finally的if中
public class TEST
{
public static void main(String arg[]){
bool exitFinally = false;
try
{
System.out.println("execute try");
//what should I write hear that finally does not run.
}
catch (Exception e){
System.out.println(e);
}
finally{
if(exitFinally)
return;
System.out.println("execute finally");
}
}
}
public class TEST
{
public static void main(String arg[]){
boolean b = true;
try
{
System.out.println("execute try");
if (something()) b = false;
}
catch (Exception e){
System.out.println(e);
}
finally{
if (b){
System.out.println("execute finally");
}
}
}
}
最后将代码放入if中
public class TEST
{
public static void main(String arg[]){
boolean b = true;
try
{
System.out.println("execute try");
if (something()) b = false;
}
catch (Exception e){
System.out.println(e);
}
finally{
if (b){
System.out.println("execute finally");
}
}
}
}
使用布尔标志:
public class TEST
{
public static void main(String arg[]){
boolean success=false;
try
{
System.out.println("execute try");
//what should I write hear that finally does not run.
success=true;
}
catch (Exception e){
System.out.println(e);
}
finally{
if (!success)
{
System.out.println("execute finally");
}
}
}
}
使用布尔标志:
public class TEST
{
public static void main(String arg[]){
boolean success=false;
try
{
System.out.println("execute try");
//what should I write hear that finally does not run.
success=true;
}
catch (Exception e){
System.out.println(e);
}
finally{
if (!success)
{
System.out.println("execute finally");
}
}
}
}
finally意味着无论异常是否发生都要执行,period。
这是无法避免的,除非像约阿希姆所说的那样采取可疑的策略
如果finally块中的代码不是每次都要执行,那么不要使用finally构造;使用一个简单的if构造来代替无论是否发生异常,finally都要执行。
这是无法避免的,除非像约阿希姆所说的那样采取可疑的策略
如果finally块中的代码不是每次都要执行,那么不要使用finally构造;改为使用简单的if构造System.exit与使用布尔标志的其他答案之间的关系如何?您能否提供如何使用.exit的详细信息以及询问者的try-catch-finally代码?System.exit与使用布尔标志的其他答案有何关联?您能否提供如何使用.exit的详细说明以及询问者的try-catch-finally代码?您从未将exitFinally设置为true,并且finally块仍在执行,即使它在结束之前。如果将exitFinally设置为true,则应编写if!退出;这个想法是在适当的建议中设置Exitfinal为true-我确实将其中的一些留给OP。Yes最终仍然执行,但如果需要,会立即返回。没有实现finally的其他方法无法解决此问题。您从未将exitFinally设置为true,并且finally块仍在执行,即使它在结束之前。如果将exitFinally设置为true,则应编写if!退出;这个想法是在适当的建议中设置Exitfinal为true-我确实将其中的一些留给OP。Yes最终仍然执行,但如果需要,会立即返回。另一个问题是最终没有实现。类名以大写字母开头,然后是单词边界上的小写和大写字母。这是一个奇怪的问题。您不希望finally块运行,那么finally块是什么呢?不要使用System.out.printlne;相反,使用e.printStackTrace.Class名称以大写字母开头,然后是单词边界上的小写和大写字母。这是一个奇怪的问题。您不希望finally块运行,那么finally块是什么呢?不要使用System.out.printlne;在这些示例中,finally块仍然运行,您只是有条件地执行其中的一些行。在这些示例中,finally块仍然运行,您只是有条件地执行其中的一些行。