为什么有时不声明 throws 也可以编译通过

博主发布

RuntimeException不需要强制对其进行声明和捕获。可以认为Checked exception就是要强制程序员去处理这个异常(不管你throws多少层,终归要在某个地方catch),而Runtime exception则没有这个限制,可以自由选择是否catch

// 定义的是一个必检异常
class HexFormatException extends Exception { }

// 定义的是一个运行时异常
class BinaryFormatException extends NumberFormatException {}

NumberFormatException extends IllegalArgumentException

IllegalArgumentException extends RuntimeException

分类: Java

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。