使用Java异常捕获,字符串数组String[] str = {"邓超","李晨","鹿晗","杨颖"}
当取得第五个数组元素时,显示输出"没有第五个数组元素,发生异常"。
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try{
- String[] str = {"邓超","李晨","鹿晗","杨颖"};
- str[5]="124";
- }catch(ArrayIndexOutOfBoundsException e){
- System.out.println("数组越界");
- }
- }
- }