在 Java 异常处理中,throws 是声明异常的关键字,很多初学者甚至有一定经验的开发者都会踩坑。下面按常见误区 → 正确理解 → 示例来说明。
throws 会“处理”异常 ❌错误理解:
方法上加了
throws,异常就被处理了。
正确理解:
throws 只是声明“这个方法可能抛异常”,并不处理异常。
真正的处理是 try-catch。
void test() throws Exception {
throw new Exception("error");
}
✅ 异常仍然会抛给调用者。
throws 和 throw 是同一个东西 ❌| 关键字 | 作用 |
|---|---|
throw |
抛出一个异常对象 |
throws |
声明方法可能抛出的异常类型 |
❌ 错误示例:
void test() throws new Exception(); // 编译错误
✅ 正确示例:
void test() throws Exception {
throw new Exception();
}
throws ❌正确理解:
throws 或 try-catchvoid a() throws IOException { } // 必须
void b() { throw new RuntimeException(); } // 不需要
常见 RuntimeException:
NullPointerExceptionIndexOutOfBoundsExceptionIllegalArgumentExceptionmain 方法不处理异常也没事 ❌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 { }
问题:
✅ 推荐:
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 使用场景免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。