gpt4 book ai didi

java - 专业程序员使用哪种风格,使用 try 和 catch 还是在代码中添加错误处理?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:50:13 26 4
gpt4 key购买 nike

<分区>

应该优先选择哪一个?示例:

class Example {
int[] A = new A[5];

void setArray(int item, int index) {
if(index < 0 || index >= 5) {
System.out.println("Index out of bounds");
} else {
A[index] = item;
}
}
}

还是这个?

class Example {
int[] A = new A[5];

void setArray(int item, int index) {
try {
A[index] = item;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds");
}
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com