问题描述
你可以解释一下 throw
, throws
和之间的区别是什么?Throwable
以及何时使用哪个?
-
throws
:在编写方法时使用,声明有问题的方法引发指定(检查)异常。
检查异常,运行时异常(NullPointerExceptions等)可能会抛出,而不使用方法声明
throws NullPointerException
。 -
throw
:实际抛出异常的指令。 (或更具体地说,Throwable )。
throw关键字后跟引用
Throwable
(通常是例外)
示例:
-
Throwable
:必须扩展才能创建的类
示例:
Can any of you explain what the differences are between
throw
,throws
andThrowable
and when to use which?解决方案throws
: Used when writing methods, to declare that the method in question throws the specified (checked) exception.As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare
throws NullPointerException
.throw
: Instruction to actually throw the exception. (Or more specifically, the Throwable).The throw keyword is followed by a reference to a
Throwable
(usually an exception).
Example:
Throwable
: A class which you must extend in order to create your own, custom, throwable.
Example:
这篇关于异常处理:throw,throws和Throwable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 10:20 -