
在 try-catch 块中嵌套 for 循环时,异常处理对循环执行流程的影响至关重要。本文将通过一个汽车租赁服务的示例,详细解释当循环内部抛出异常时,如何正确地使用 try-catch 块来保证循环的完整执行。
当 for 循环位于 try 块内部时,如果循环体内的代码抛出异常,catch 块将会捕获该异常。关键在于 try-catch 块的位置决定了异常处理的范围。
错误示例:try-catch 块位于 for 循环外部
如果 try-catch 块包围整个 for 循环,那么当循环的某一次迭代抛出异常时,程序会跳转到 catch 块执行,并且不会继续执行 for 循环的后续迭代。
例如,以下代码会导致循环在第一次遇到异常时终止:
try {
for (int i = 1; i <= num; i++) {
// 可能抛出异常的代码
}
} catch (Exception e) {
System.out.println(e.getMessage());
}正确示例:try-catch 块位于 for 循环内部
为了保证 for 循环能够完整执行,即使在某次迭代中发生异常,也应该将 try-catch 块放置在 for 循环的内部。这样,每次迭代都会被 try-catch 块保护,当发生异常时,catch 块会处理该异常,并且循环会继续执行下一次迭代。
以下代码展示了正确的异常处理方式:
for (int i = 1; i <= num; i++) {
try {
// 可能抛出异常的代码
} catch (Exception e) {
System.out.println(e.getMessage());
}
}假设有一个汽车租赁服务,需要处理多个租车请求。每个请求可能因为乘客人数不合法或目的地不存在而抛出异常。以下代码展示了如何正确地处理这些异常,并保证所有租车请求都能被处理:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class ImproperHeadCountException extends Exception {
public ImproperHeadCountException(String message) {
super(message);
}
}
class WrongDestinationException extends Exception {
public WrongDestinationException(String message) {
super(message);
}
}
class CarRental {
private int passenger_count;
private String chosen_destination;
private static Map<String, Double> available_destinations = new HashMap<>();
static {
available_destinations.put("Elliot's Beach", 5000.0);
available_destinations.put("Marina Beach", 4000.0);
}
public CarRental(int passenger_count, String chosen_destination) {
this.passenger_count = passenger_count;
this.chosen_destination = chosen_destination;
}
public void carBooker() throws ImproperHeadCountException, WrongDestinationException {
try {
if (this.passenger_count < 1) {
throw new ImproperHeadCountException("Head count should be positive non zero value");
}
if (!available_destinations.containsKey(this.chosen_destination)) {
throw new WrongDestinationException("Invalid destination");
}
double fare_per_head = available_destinations.get(this.chosen_destination) / this.passenger_count;
System.out.println("Destination: " + this.chosen_destination + ", Head cost: " + fare_per_head);
} catch (ImproperHeadCountException e) {
System.out.println(e.getMessage());
} catch (WrongDestinationException e) {
System.out.println(e.getMessage());
}
//catch (NullPointerException e) {} // 可以移除,因为已经通过containsKey判断
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num = s.nextInt(); // input the number of car rental requests
s.nextLine(); // Consume newline
for (int i = 1; i <= num; i++) {
try {
int heads = s.nextInt(); // enter head count of passengers
s.nextLine(); // Consume newline
String dest = s.nextLine(); // enter destination
CarRental obj = new CarRental(heads, dest);
obj.carBooker();
} catch (Exception e) {
System.out.println(e.getMessage()); // 处理其他可能的异常,例如输入格式错误
}
}
s.close();
}
}代码解释:
注意事项:
在包含 try 块的 for 循环中,try-catch 块的位置决定了异常处理的范围和循环的执行流程。为了保证循环能够完整执行,即使在某次迭代中发生异常,也应该将 try-catch 块放置在 for 循环的内部。通过合理地使用 try-catch 块,可以有效地处理异常,并保证程序的稳定性和可靠性。
以上就是在包含 try 块的 for 循环中处理异常的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号