当前位置:网站首页>Take a look at try{}catch{}

Take a look at try{}catch{}

2022-07-19 13:58:00 Dark matter ghost

The third day of fishing , Today I asked my tutor what to do today , He said to read the document , It's been three days . I can't sit down , I want to pull the code directly to sort out the business . But inside and outside the company's distribution ip Never come down , Open niuke.com and brush the questions .

Encountered a very interesting question

Given the following code , The output is ()
public class Test {
    
  public static void main(String[] args) {
    
    try {
    
      test();
      System.out.println("A");
    } catch (ArrayIndexOutOfBoundsException e) {
    
      System.out.println("B");
      return;
    } catch (Exception e) {
    
      System.out.println("C");
    } finally {
    
      System.out.println("D");
    }
  }
 
  private static void test() {
    
    int[] a = {
    1, 2, 4};
    System.out.println(a[3]);
  }
}

 Insert picture description here
I was a little confused at first , My solution to the problem .

test() Error reporting captured , therefore A Will not print exclusions AD answer , stay try{}catch(){}finally{} In the sentence , Anyway finally The code block of is bound to execute , Included in catch The statement contains return sentence , stay return It will be executed before finally. So choose B,
But what I am struggling with is whether to implement it C, In fact, if the answer is BCD I'll choose this one if you like , Fortunately, I chose the right one .

use Baidu Search : stay try{}catch(){}catch(){}finally{} In the sentence , Multiple catch The code in is executed sequentially , If the most appropriate exception is matched, the code block will be executed and will not be executed further , So the print result can only be BD,

Ask every day , If finally There are also return, Then the function will return catch Medium return still finally What about the middle ? The comments section tells me

原网站

版权声明
本文为[Dark matter ghost]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207171944374929.html