java异常会终止整个java应用程序吗?

编程入门 行业动态 更新时间:2024-10-06 22:27:58
本文介绍了java异常会终止整个java应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我以前认为当发生异常时,整个java应用程序将被终止。例如,我写了一个测试函数来测试我的想法。

public void test(){ File fileDir =新的File(sourceDataDir); if(fileDir.exists()){ File [] files = fileDir.listFiles(); (int index = 0; index< files.length; index ++){ System.out.println(index =+ index); 文件文件= files [index]; if(index == 1)//删除文件导致FileNotFoundException file.delete(); try { BufferedReader in = new BufferedReader(new FileReader(file)); } catch(FileNotFoundException e){ e.printStackTrace(); } } } }

删除文件以手动导致 FileNotFoundException 。我以前认为整个应用程序将在异常发生时终止。但事实上,应用程序将继续读取剩余的文件。所以,我的问题是,在什么情况下,异常会导致整个应用程序被终止?

解决方案

我的问题是,在什么情况下,异常会导致整个应用程序被终止?

永远不会。只有System.exit()导致整个程序终止(并且JVM崩溃)

想想一个例外,就像一个强大的 break; / code>可以突破方法。中断可能导致循环退出,如果唯一的非守护线程中的最后一个循环,程序将退出。但是它不会导致它,尽管它可能是您在程序死亡之前看到的最后一件事。

I used to think that when an exception happened, the whole java application will be terminated. For example, I write a test function to test my idea.

public void test(){ File fileDir=new File(sourceDataDir); if(fileDir.exists()){ File[] files = fileDir.listFiles(); for(int index=0 ; index<files.length ; index++){ System.out.println("index = "+index); File file = files[index]; if(index == 1)//delete a file to cause a FileNotFoundException file.delete(); try { BufferedReader in = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } } }

I delete a file to cause a FileNotFoundException manually. I used to think that the whole application will terminate when the exception happened. But in fact, the application will continue reading the remaining files. So, my question is, in what condition an exception will cause the whole application to be terminated?

解决方案

my question is ,in what condition ,a exception will cause the whole application to be terminated?

It won't ever. Only System.exit() causes the whole program to terminate (and a JVM crash)

Think of an Exception and like a powerful break; which can break out of methods. A break could result in a loop exiting and if the last loop in the only non daemon thread, the program will exit. But it doesn't cause it, even though it might be the last thing you see before you program dies.

更多推荐

java异常会终止整个java应用程序吗?

本文发布于:2023-11-05 10:29:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   异常   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!