为什么ArrayIndexOutOfBoundsException不是编译时错误? [英] Why isn't the ArrayIndexOutOfBoundsException a compile time error?

查看:97
本文介绍了为什么ArrayIndexOutOfBoundsException不是编译时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么 ArrayIndexOutOfBoundsException 是运行时异常而不是编译时错误吗?
在明显的情况下,当索引为负数或大于数组大小时,我不明白为什么它不能成为编译时错误。

Can someone explain to me why ArrayIndexOutOfBoundsException is a run-time exception instead of a compile-time error? In obvious cases when the indexes are negative or greater than the array size, I don't see why it cannot be a compile-time error.

:尤其是在编译时知道数组的大小甚至是索引的情况下,例如 int [] a = new int [10]; a [-1] = 5; 这应该是编译错误。

Edited: especially when the size of the array and even the indexing is known at compile time, for example int[] a = new int[10]; a[-1]=5; This should be a compilation error.

推荐答案

只能在运行时定义数组(例如,最简单的情况是,如果数组的大小取决于用户输入)。

The size of the arrays may be defined only at runtime (for instance, the simplest case, if the size of an array depends on the user input).

因此,不可能通过在不实际知道其范围(大小)的情况下检查数组的访问,从而在编译时检查此类异常。

Therefore it would be impossible to check at compile time for such kind of exceptions, by checking the accesses of an array without actually know its bounds (size).

这篇关于为什么ArrayIndexOutOfBoundsException不是编译时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆