Java新手在這裏,我正在做一個練習,儘量使用盡可能少的try ... catch語句來捕獲拋出的異常。當我編譯代碼時,我收到IllegalArgumentException
錯誤,並不確定如何使用try ... catch語句解決此問題。嘗試... Java中的catch語句練習
我看過教程,看了其他的例子,但由於這是一個新概念,我仍然不確定如何使用try ... catch來完成這個特定的練習。
public class Main extends Object {
public static void main(String [] args) {
tryGetMax();
tryRemove();
private static final void tryGetMax() {
int max = 0;
max = FunMethods.getMax((Integer[])null);
Integer[] numbers = new Integer[50];
Random rand = new Random();
for (int i = 0; i < 50; i++) {
numbers[i] = new Integer(rand.nextInt(500));
}
numbers[32] = null;
max = FunMethods.getMax(numbers);
numbers[32] = new Integer(rand.nextInt(500));
max = FunMethods.getMax(numbers);
}
運動的第二部分:
private static final void tryRemove() {
FunMethods.remove(null, 2);
Object[] someObjects = new Object[12];
someObjects[0] = "a string!";
someObjects[1] = new Integer(32);
someObjects[2] = new Float(42.5f);
someObjects[3] = "another string";
for (int i = 4; i < someObjects.length; i++) {
someObjects[i] = String.valueOf(i);
}
FunMethods.remove(someObjects, 12);
someObjects = FunMethods.remove(someObjects, 3);
我沒有看到你的代碼,你甚至試圖添加一個try/catch語句的任何地方。您是閱讀文檔還是查看任何示例?你明白了什麼? – tnw
你說得對,我還沒有。這是我在添加try/catch語句以使其正確編譯的行上存在混淆。 – MJW
SO不適合「教我如何做基本的東西」,對不起 –