我收到了ArrayIndexOutOfBoundsException
,但我想抛出一个IllegalArgumentException
。
我尝试捕获异常并使用throws
声明似乎无法解决问题。如果发生IllegalArgumentException
,我该如何抛出ArrayIndexOutOfBoundsException
?
答案 0 :(得分:1)
您可以catch
ArrayIndexOutOfBoundsException
然后throw
IllegalArgumentException
,如下所示:
try {
//Add your code here
} catch(ArrayIndexOutOfBoundsException arrayIndexExe) {
//Change the below message according your requirement
String message ="Input Validation failed because of incorrect data";
//You can set the cause (2nd arg) below as ArrayIndexOutOfBoundsException
throw new IllegalArgumentException(message, arrayIndexExe);
}