首页 > Java > java教程 > 正文

在包含 try 块的 for 循环中处理异常

DDD
发布: 2025-08-17 17:32:29
原创
330人浏览过

在包含 try 块的 for 循环中处理异常

在 try-catch 块中嵌套 for 循环时,异常处理对循环执行流程的影响至关重要。本文将通过一个汽车租赁服务的示例,详细解释当循环内部抛出异常时,如何正确地使用 try-catch 块来保证循环的完整执行。

理解 try-catch 块与 for 循环的交互

当 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 块会处理该异常,并且循环会继续执行下一次迭代。

Check for AI
Check for AI

在论文、电子邮件等中检测AI书写的文本

Check for AI 88
查看详情 Check for AI

以下代码展示了正确的异常处理方式:

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();
    }
}
登录后复制

代码解释:

  1. 自定义异常类: 定义了 ImproperHeadCountException 和 WrongDestinationException 两个自定义异常类,分别用于表示乘客人数不合法和目的地不存在的情况。
  2. carBooker() 方法: 该方法负责处理单个租车请求,并在内部使用 try-catch 块来捕获和处理可能发生的异常。如果乘客人数小于 1,则抛出 ImproperHeadCountException;如果目的地不存在,则抛出 WrongDestinationException。
  3. main() 方法: main方法读取租车请求的数量,并使用一个 for 循环来处理每个请求。关键在于,try-catch 块被放置在 for 循环的内部,确保每个请求都能被独立处理,即使某个请求抛出异常,也不会影响其他请求的执行。 外部的try-catch是为了防止输入格式错误等其他异常导致程序崩溃。
  4. 静态初始化块: 使用静态初始化块初始化 available_destinations HashMap。

注意事项:

  • 在 catch 块中,应该对捕获到的异常进行适当的处理,例如打印错误信息、记录日志等。
  • 可以根据实际需求,定义多个 catch 块来处理不同类型的异常。
  • 如果某个异常无法在当前方法中处理,可以选择将其抛出,交给调用方处理。

总结

在包含 try 块的 for 循环中,try-catch 块的位置决定了异常处理的范围和循环的执行流程。为了保证循环能够完整执行,即使在某次迭代中发生异常,也应该将 try-catch 块放置在 for 循环的内部。通过合理地使用 try-catch 块,可以有效地处理异常,并保证程序的稳定性和可靠性。

以上就是在包含 try 块的 for 循环中处理异常的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号