异常处理 【NumberFormatException异常】编写一个程序,提示用户输入两个整数,然后显示它们的和。用户输入错误时提示用户重新输入。 【ArrayIndexOutOfBoundsException异常】编写一个程序,创建一个由100个随机选取的整数构成的数组;提示用户输入数组下标,然后显示元素的值,如果指定的下标越界,显示消息out of bounds。 【OutOfMemoryError错误】编写一个程序,它能导致JVM抛出一个OutOfMemoryError,然后捕获并处理这个错误。
时间: 2024-02-06 12:10:13 浏览: 34
【NumberFormatException异常】代码如下:
```
import java.util.Scanner;
public class NumberFormatExceptionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = 0, num2 = 0;
boolean validInput = false;
while (!validInput) {
try {
System.out.print("请输入第一个整数:");
num1 = Integer.parseInt(scanner.nextLine());
System.out.print("请输入第二个整数:");
num2 = Integer.parseInt(scanner.nextLine());
validInput = true;
} catch (NumberFormatException e) {
System.out.println("输入错误,请重新输入!");
}
}
System.out.println("两个整数的和为:" + (num1 + num2));
}
}
```
【ArrayIndexOutOfBoundsException异常】代码如下:
```
import java.util.Random;
import java.util.Scanner;
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
int[] arr = new int[100];
Random random = new Random();
for (int i = 0; i < 100; i++) {
arr[i] = random.nextInt(1000);
}
Scanner scanner = new Scanner(System.in);
boolean validInput = false;
while (!validInput) {
try {
System.out.print("请输入数组下标:");
int index = Integer.parseInt(scanner.nextLine());
System.out.println("arr[" + index + "] = " + arr[index]);
validInput = true;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界,请重新输入!");
} catch (NumberFormatException e) {
System.out.println("输入错误,请输入一个整数!");
}
}
}
}
```
【OutOfMemoryError错误】代码如下:
```
public class OutOfMemoryErrorExample {
public static void main(String[] args) {
int[] arr = new int[Integer.MAX_VALUE];
}
}
```
注意,上面的代码会导致JVM抛出一个OutOfMemoryError,并且会使程序崩溃。为了处理这个错误,我们可以使用try-catch语句来捕获并处理它,如下所示:
```
public class OutOfMemoryErrorExample {
public static void main(String[] args) {
try {
int[] arr = new int[Integer.MAX_VALUE];
} catch (OutOfMemoryError e) {
System.out.println("JVM抛出了一个OutOfMemoryError!");
e.printStackTrace();
}
}
}
```
当JVM抛出OutOfMemoryError时,try块中的代码就会终止执行,然后程序将跳转到catch块中,显示错误信息并打印堆栈跟踪。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)