在下面的代码中,没有为IndexOutOfRangeException-声明任何对象
class ExcDemo1 {
static void Main() {
int[] nums = new int[4];
try {
Console.WriteLine("Before exception is generated.");
// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) {
nums[i] = i;
Console.WriteLine("nums[{0}]: {1}", i, nums[i]);
}
Console.WriteLine("this won't be displayed");
}
catch (IndexOutOfRangeException) {
// Catch the exception.
Console.WriteLine("Index out-of-bounds!");
}
Console.WriteLine("After catch block.");
}
}
我只想知道是否可以?如果是的话,我们也可以在Java中做同样的事情吗?这样做的好处是什么?
您的问题很模糊。我猜您正在尝试询问是否允许我们在不在下面的代码中为其声明变量的情况下捕获异常。catch (IndexOutOfRangeException) { // Catch the exception. Console.WriteLine("Index out-of-bounds!"); }
如果这是您的问题?那是的,我们可以做到。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句