我在使用java覆盖异常时遇到问题

时间:2016-12-04 17:49:03

标签: java

我收到了ArrayIndexOutOfBoundsException,但我想抛出一个IllegalArgumentException

我尝试捕获异常并使用throws声明似乎无法解决问题。如果发生IllegalArgumentException,我该如何抛出ArrayIndexOutOfBoundsException

1 个答案:

答案 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);
  }