下面的异常处理代码的输出结果是( )。 try{ int result = 6/0; System.out.print("try,"); } catch(ArithmeticException e1) { System.out.print("ArithmeticException异常,"); throw new Exception(); } catch(Exception e2) { System.out.print("Exception异常,"); } finally { System.out.print("finally"); }
时间: 2024-04-06 21:31:32 浏览: 109
输出结果为 "ArithmeticException异常,Exception异常,finally"。
首先,try块中执行了一个除以0的操作,抛出了ArithmeticException异常。然后,第一个catch块捕获到了这个异常,输出了 "ArithmeticException异常,",并且抛出了一个新的Exception异常。由于第二个catch块能够捕获到Exception异常,因此它会被执行,输出 "Exception异常,"。最后,无论是否发生异常,finally块中的代码都会执行,输出 "finally"。
相关问题
62、(1分)编译并运行如下Java程序,将输出() public static void main(Stringl args) ( try ( int num1 = 2; int num2 = 0; int result = num1 / num2; System.out,println(result); throw new NumberFormatException( ); ) catch (ArrayIndexOutOfBoundsException e)System.out,print(“1"); )catch (NumberFormatException e) ( System.out.print("2"); )catch (Exception e) (
答案:
编译该程序会出现语法错误,因为try语句块中的资源声明需要使用分号(;)分隔,而不是逗号(,)。因此,修改后的程序应该是这样的:
```java
public static void main(String[] args) {
try (int num1 = 2; int num2 = 0; int result = num1 / num2) {
System.out.println(result);
throw new NumberFormatException();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.print("1");
} catch (NumberFormatException e) {
System.out.print("2");
} catch (Exception e) {
System.out.print("3");
}
}
```
在运行该程序时,会抛出ArithmeticException异常,因为在try语句块中除数为0,导致除法运算异常。因此,最终输出的结果是2。
修改代码public class CLI { private static Scanner input = new Scanner(System.in); public static void main(String[] args){ Library library = new Library("UIC Library"); String names; int number ; int borrow; int totalBorrowedBooks = 50; // initial value int action = 0; Scanner scanner = new Scanner(System.in); while (action != 6) { System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): "); String input = scanner.nextLine(); try { action = Integer.parseInt(input); } catch (NumberFormatException e) { System.out.println("You must type an integer!"); continue; } Object bookCounts; switch (action) { case 4: System.out.println("Enter the name of the user: "); names = scanner.next(); System.out.println("Enter the number of books: "); number = scanner.nextInt(); if(!names.equals("Anna") && !names.equals("Bob")) { System.out.println("User "+ names +" unknown."); break;//add break to end this operation. } System.out.println("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):"); borrow = scanner.nextInt(); System.out.print("Enter the name of the user:"); names = scanner.next(); if (names.equals("Anna")) { number -= borrow; if (number < -5) { System.out.println("You can't borrow more books. Return some books first."); } else { System.out.println(names+" borrows " + (-5 + number -1) + "book(s)."); } }else if(names.equals("Bob")) { number -= borrow; if (number > 10) { System.out.println("You can't borrow more books. You have reached the maximum limit."); } else if(number>0||number<10){ System.out.println(names+" borrows " + (10 - number +1) + " book(s)."); } else if(number<0){ System.out.println("Positive integers only!"); System.out.println("Enter the number of books:"); number = scanner.nextInt(); System.out.println(names+" borrows " + (10 - number +1) + " book(s)."); // Directly output the result after modification. } }else { System.out.println("User "+ names +" unknown."); } break;
public class CLI {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args){
Library library = new Library("UIC Library");
String names;
int number;
int borrow;
int totalBorrowedBooks = 50; // initial value
int action = 0;
Scanner scanner = new Scanner(System.in);
while (action != 6) {
System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): ");
String input = scanner.nextLine();
try {
action = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("You must type an integer!");
continue;
}
switch (action) {
case 4:
System.out.println("Enter the name of the user: ");
names = scanner.next();
System.out.println("Enter the number of books: ");
number = scanner.nextInt();
if(!names.equals("Anna") && !names.equals("Bob")) {
System.out.println("User "+ names +" unknown.");
break;
}
System.out.println("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");
borrow = scanner.nextInt();
System.out.print("Enter the name of the user:");
names = scanner.next();
if (names.equals("Anna")) {
number -= borrow;
if (number < -5) {
System.out.println("You can't borrow more books. Return some books first.");
break;
} else {
System.out.println(names+" borrows " + (-5 + number -1) + "book(s).");
break;
}
} else if(names.equals("Bob")) {
number -= borrow;
if (number > 10) {
System.out.println("You can't borrow more books. You have reached the maximum limit.");
break;
} else if(number>0||number<10){
System.out.println(names+" borrows " + (10 - number +1) + " book(s).");
break;
} else if(number<0){
System.out.println("Positive integers only!");
System.out.println("Enter the number of books:");
number = scanner.nextInt();
System.out.println(names+" borrows " + (10 - number +1) + " book(s).");
break;
}
} else {
System.out.println("User "+ names +" unknown.");
break;
}
default:
break;
}
}
}
}
阅读全文