验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

Java Throws常见误区有哪些

阅读:172 来源:乙速云 作者:代码code

Java Throws常见误区有哪些

在 Java 异常处理中,throws声明异常的关键字,很多初学者甚至有一定经验的开发者都会踩坑。下面按常见误区 → 正确理解 → 示例来说明。


误区一:throws 会“处理”异常 ❌

错误理解

方法上加了 throws,异常就被处理了。

正确理解
throws 只是声明“这个方法可能抛异常”,并不处理异常。
真正的处理是 try-catch

void test() throws Exception {
    throw new Exception("error");
}

✅ 异常仍然会抛给调用者。


误区二:throwsthrow 是同一个东西 ❌

关键字 作用
throw 抛出一个异常对象
throws 声明方法可能抛出的异常类型

❌ 错误示例:

void test() throws new Exception(); // 编译错误

✅ 正确示例:

void test() throws Exception {
    throw new Exception();
}

误区三:所有异常都必须 throws

正确理解

  • 受检异常(Checked Exception):必须 throwstry-catch
  • 运行时异常(RuntimeException):不需要
void a() throws IOException { }   // 必须
void b() { throw new RuntimeException(); } // 不需要

常见 RuntimeException:

  • NullPointerException
  • IndexOutOfBoundsException
  • IllegalArgumentException

误区四:main 方法不处理异常也没事 ❌

public static void main(String[] args) throws Exception {
    // 异常会直接抛给 JVM
}

✅ 能编译
❌ 程序会直接崩溃,打印堆栈信息

生产代码不推荐这样写。


误区五:throws 可以代替 try-catch

void read() throws IOException {
    Files.readAllBytes(Path.of("a.txt"));
}

如果你不关心异常,可以。
但如果你需要:

  • 记录日志
  • 回滚事务
  • 返回默认值

✅ 应该用 try-catch


误区六:throws 的异常范围可以“缩小” ❌

重写方法时
子类方法 不能抛出比父类更宽泛的受检异常

❌ 错误示例:

class Parent {
    void test() throws IOException {}
}

class Child extends Parent {
    @Override
    void test() throws Exception {} // 编译错误
}

✅ 可以:

  • 不抛异常
  • 抛出相同异常
  • 抛出子异常

误区七:一个方法只能 throws 一个异常 ❌

✅ 可以声明多个异常:

void test() throws IOException, SQLException, InterruptedException {
}

误区八:throws 会影响业务逻辑 ❌

throws 只影响编译期检查,不影响运行逻辑。

void test() throws Exception {
    System.out.println("hello");
}

✅ 和下面逻辑完全一样:

void test() {
    System.out.println("hello");
}

只有真的抛异常才会有影响。


误区九:只要 throws Exception 就万事大吉 ❌

❌ 这是最差实践

void process() throws Exception { }

问题:

  • 掩盖真实异常类型
  • 调用者无法判断如何处理
  • 破坏 API 可读性

✅ 推荐:

void process() throws IOException, SQLException

误区十:finally 会受 throws 影响 ❌

void test() throws Exception {
    try {
        throw new Exception();
    } finally {
        System.out.println("finally");
    }
}

finally 一定执行
throws 不影响 finally


一句话总结 ✅

throws 只是“甩锅”,不是“解决问题”

如果你愿意,我可以:

  • 面试问答形式总结
  • 对比 throws vs try-catch 使用场景
  • 结合 Spring / 实际项目讲最佳实践
分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>