异常处理:throw,throws和Throwable [英] Exception handling : throw, throws and Throwable

查看:176
本文介绍了异常处理:throw,throws和Throwable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以解释一下 throw throws 之间的区别是什么?Throwable 以及何时使用哪个?

解决方案


  • throws :在编写方法时使用,声明有问题的方法引发指定(检查)异常。



    检查异常,运行时异常(NullPointerExceptions等)可能会抛出,而不使用方法声明 throws NullPointerException


  • throw :实际抛出异常的指令。 (或更具体地说, Throwable )。



    throw关键字后跟引用 Throwable (通常是例外)




示例:









  • Throwable :必须扩展才能创建的类



示例:









Can any of you explain what the differences are between throw, throws and Throwable 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆