【发布时间】:2017-06-20 02:29:46
【问题描述】:
我正在学习 java 中的异常。我遇到了以下问题:
String bigstring = myscanner.nextLine();
String[] splited = bigstring.split("\\s+");
try {
smallstring1 = splited[0];
smallstring2 = splited[1];
smallstring3 = splited[2];
} catch(java.lang.ArrayIndexOutOfBoundsException exc) {
smallstring3 = null;
}
如果用户只想输入 2 个单词,这将起作用。
如果他想打一个字怎么办?
我们能否以某种方式指定冒号后出错的值?
喜欢:
java.lang.ArrayIndexOutOfBoundsException: 2
或
java.lang.ArrayIndexOutOfBoundsException: 1
我们可以在 try/catch 块中以某种方式使用(对于这个例子)这个“2”或“1”吗?
【问题讨论】:
标签: java exception try-catch indexoutofboundsexception