我有一个检查二维数组中点的方法,它也检查它们是否为空。我想抛出ArrayIndexOutOfBoundsException
,因为我已经检查了null。如何抛出ArrayIndexOutOfBoundsException?
我尝试在声明方法后添加throws ArrayIndexOutOfBoundsException
,但它不起作用。我该怎么做呢?
我有一个检查二维数组中点的方法,它也检查它们是否为空。我想抛出ArrayIndexOutOfBoundsException
,因为我已经检查了null。如何抛出ArrayIndexOutOfBoundsException?
我尝试在声明方法后添加throws ArrayIndexOutOfBoundsException
,但它不起作用。我该怎么做呢?
throws
在方法定义中说该方法可以抛出异常。要真正把它扔在方法体中,使用throw new ArrayIndexOutOfBoundsException();
试试这个:
throw new ArrayIndexOutOfBoundsException("this is my exception for the condition");
基本上throws
关键字告诉我们,该方法可以抛出异常。如果你想抛出任何类型的异常,你需要调用该类型的构造函数。
throw new NullPointerException("Null Pointer Exception");
你的方法写的声明后:
private returnType methodName(CommunicationObject requestObject)
throws ArrayIndexOutOfBoundException {
}
如果你只是列出的功能是能够抛出一个异常,但实际上从未抛出异常的功能,是不断产生也不例外。
如果抛出异常但未列出可引发异常的函数,则可能会收到编译器错误或有关未捕获异常的警告。
你需要列出你的函数抛出一个ArrayIndexOutOfBoundsException并抛出异常在你的函数中的某处。
例如:
public ... myArrayFunction(...) throws ArrayIndexOutOfBoundsException {
.... // handle the array
if (some condition) {
throw new ArrayIndexOutOfBoundsException("Array Index Out of Bounds");
}
}
你有多少呢?在这里粘贴一些代码。 :) – HashimR 2012-03-29 04:19:14