简体   繁体   English

java中try-catch的完整过程是怎样的?

[英]What is the complete process of try-catch in java?

I have been experimenting with try-catch and have been confused on specifically how it works.我一直在试验 try-catch 并且对它的具体工作方式感到困惑。

try                                     // x is array of 10 doubles set to 0
        {
            for (int a = 0; a < 11; a+= 2)
                x[a] = 5;
        }
        catch(Exception e)
        {
            System.out.println("error");
        }

In this instance, all values in the array can be reached, but the code breaks at x[10].在这种情况下,可以访问数组中的所有值,但代码在 x[10] 处中断。 So, have all the values been set to 5?那么,所有值都设置为 5 了吗?

try                             // x is array of 10 doubles set to 0
        {
            for (int a = -1; a < 11; a+= 2)
                x[a] = 5;
        }
        catch(Exception e)
        {
            System.out.println("error");
        }

In this instance, it will try x[-1] and catch the error.在这种情况下,它将尝试 x[-1] 并捕获错误。 So, will it not go back and complete the loop (x[0], x[1], ... x[9] )?那么,它不会返回并完成循环 (x[0], x[1], ... x[9] ) 吗? So, all values still remain 0?那么,所有值仍然保持为 0 吗?

Is this how the try-catch works?这是 try-catch 的工作方式吗?
Any help would be appreciated.任何帮助,将不胜感激。
Thank you.谢谢你。

Yeah, both examples would throw ArrayIndexOutOfBoundsException .是的,两个例子都会抛出ArrayIndexOutOfBoundsException

So, The try block contains a block of program statements within which an exception might occur.因此, try 块包含一个程序语句块,其中可能会发生异常。 A try block is always followed by a catch block , which handles the exception that occurs in associated try block . try 块后面总是跟一个catch 块,它处理在关联的try 块中发生的异常 A try block must followed by a Catch block or Finally block or both . try 块之后必须跟一个Catch 块finally 块两者

A catch block must be associated with a try block . catch 块必须与try 块相关联。 The corresponding catch bloc k executes if an exception of a particular type occurs within the try block .如果在try 块中发生特定类型的异常,则执行相应的catch For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception execute.例如,如果在try块出现算术异常然后封装在catch块算术异常执行的语句。

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))‏
{
     //error handling code
}

If an exception occurs in try block then the control of execution is passed to the catch block from try block .如果在try 块中发生异常,则执行控制从try 块传递到catch The exception is caught up by the corresponding catch block .异常由相应的catch 块捕获 A single try block can have multiple catch statements associated with it, but each catch block can be defined for only one exception class .单个try 块可以有多个与之关联的catch 语句,但每个catch 块只能为一个异常类定义。 The program can also contain nested try-catch-finally blocks .该程序还可以包含嵌套的try-catch-finally 块

After the execution of all the try blocks , the code inside the finally block executes.在执行完所有try 块之后finally 块中的代码就会执行。 It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch blocks .根本不强制包含finally 块,但是如果您这样做了,无论try 和 catch 块是否抛出和处理异常它都会运行

Try-Catch试着抓

The code within the try block is code that will run but if the code in the try block throws an exception/error then it is caught in catch block. try 块中的代码是将运行的代码,但如果 try 块中的代码抛出异常/错误,则它会在 catch 块中捕获。

The issue with your code is you are using the x as an array and you may not be able to set a specific spot in an array, to set that you could do something like x.push or x.put I am not sure on the specific property as I do not know java well.您的代码的问题是您将 x 用作数组,并且您可能无法在数组中设置特定位置,以设置您可以执行诸如 x.push 或 x.put 之类的操作我不确定具体属性,因为我不太了解java。 The other issue could be that your index is out of bounds, which means that you may have an array of 10 items that is zero based and you are trying to access an 11th item.另一个问题可能是您的索引超出范围,这意味着您可能有一个包含 10 个项目的数组,这些项目从零开始,而您正试图访问第 11 个项目。 so your loop should be something like:所以你的循环应该是这样的:

    {
        for (int a = 0; a < x.length; a+= 2)
            x[a] = 5;
    }

When a statement throws an exception inside of a try block, the next statement is the first statement of the catch block.当一条语句在 try 块内抛出异常时,下一条语句是 catch 块的第一条语句。

In the first example, the first few iterations of the for loop happen as normal and elements 0, 2, 4, 6, and 8 are set to 5 .在第一个示例中,for 循环的前几次迭代正常进行,元素 0、2、4、6 和 8 设置为5 On the iteration where a is 10 , the attempt to access x[10] the runtime throws an ArrayIndexOutOfBoundsException and jumps to the first statement of the catch block.a10的迭代中,尝试访问x[10]运行时会抛出ArrayIndexOutOfBoundsException并跳转到 catch 块的第一条语句。 The rest of x is unmodified. x的其余部分未修改。

In the second example, the first iteration of the for loop causes the runtime to throw an ArrayIndexOutOfBoundsException (due to the attempt to access x[-1] ) and jump to the first statement of the catch block.在第二个示例中,for 循环的第一次迭代导致运行时抛出ArrayIndexOutOfBoundsException (由于尝试访问x[-1] )并跳转到 catch 块的第一条语句。 The effect is that x is completely unmodified.效果是x完全未修改。

For more information about exceptions in Java, read the The Java Tutorials: Exceptions .有关 Java 中异常的更多信息,请阅读The Java Tutorials: Exceptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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