Java Try-Catch - 它是如何执行的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13201829/
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 11:51:22  来源:igfitidea点击:

Java Try-Catch - how does it execute?

javatry-catch

提问by user1360809

I am just wondering what the Java VM does with a Try-Catch. How does it execute it?

我只是想知道 Java VM 用 Try-Catch 做了什么。它是如何执行的?

My best guess is that it is like a Linux system which when installing something does a test run and if no errors are found asks the user if he/she wants to proceed. In the case of a Try-Catch does it do a test run and if all is OK implement it?

我最好的猜测是它就像一个 Linux 系统,在安装某些东西时会进行测试运行,如果没有发现错误,则询问用户是否要继续。在 Try-Catch 的情况下,它是否会进行测试运行,如果一切正常,则实施它?

采纳答案by thedayofcondor

It is much simpler than that - if any clause included in the try clause generates an error, the code in the catch clause (corresponding to that error - you can have multiple catch for a single try) will be executed. There is no way to know in advance if a particular clause will fail or not, only to try to recover after the error happens.

它比这简单得多——如果包含在 try 子句中的任何子句产生错误,catch 子句中的代码(对应于该错误 - 一次 try 可以有多个 catch)将被执行。没有办法提前知道特定子句是否会失败,只能在错误发生后尝试恢复。

If you have ten clauses and the last one throws an error, the modifications performed by the first 9 will not be "reverted"!

如果你有 10 个子句,最后一个抛出错误,那么前 9 个执行的修改将不会被“还原”!

回答by kosa

try
{
//execute your code
}catch(Exception e)
{
//Log message 
//IF you don't want to continue with the logic inside method, rethrow here.
}finally{
 //Ir-respective of what the status is in above cases, execute this code.
}
//Continue with other logic in the method

.This is useful in cases where you want to determine part of your code need to be executed (or) not in exception case.

.这在您想确定需要执行(或)不在异常情况下执行部分代码的情况下很有用。

To understand more about how try-catch works in java read this tutorial.

要了解有关 try-catch 在 Java 中如何工作的更多信息,请阅读本教程

回答by saum22

The code which can give some unexpected result which the program can't handle is kept inside the try block,and to handle/catch the unexpected crashing of the program we use catch block.Where we declare type of Exception the code can throw

可以给出程序无法处理的一些意外结果的代码保存在 try 块中,并使用 catch 块来处理/捕获程序的意外崩溃。在我们声明代码可以抛出的异常类型的地方

Eg:

例如:

 int a;
 try {
  a=1/0;//program wil crash here
 }catch(Exception e){
  System.out.println("Division by zero ");//handling the crash here
 }

回答by Mordechai

It starts executing normally, w/o any checking. If any exception occurs, it will break out of the clause (like breakin a loop), and immediately execute the catch, if no exception was found, it will skip the catch clause.

它开始正常执行,无需任何检查。如果发生任何异常,它将跳出子句(就像break在循环中一样),并立即执行catch,如果没有发现异常,它将跳过 catch 子句。

The finallyclause, insures that it will be called no matter if an exception was thrown, or not. A practical example would be reading from a file or network, and close the streams, no matter if an exception was thrown or not.

finally子句确保无论是否引发异常都会调用它。一个实际的例子是从文件或网络读取,并关闭流,无论是否抛出异常。