当前位置:首页 > 服务端 > Chapter 6 : Exceptions

Chapter 6 : Exceptions

Chapter 6 : Exceptions _ JavaClub全栈架构师技术笔记

  • A runtime exception is defined as the RuntimeException class and its subclasses. Runtime exceptions tend to be unexpected but not necessarily fatal. also known as unchecked exceptions.

  • A checked exception includes Exception and all subclasses that do not extend RuntimeException. Checked exceptions tend to be more anticipated

Chapter 6 : Exceptions _ JavaClub全栈架构师技术笔记

Classes listed in the throws part of a method declaration must extend java.lang.Throwable. This includes Error, Exception, and RuntimeException.


handle or declare rule

For checked exceptions, Java requires the code to either handle them or declare them in the method signature.

  • Checked exceptions are required to be handled or declared.
  • Runtime exceptions are allowed to be handled or declared.
  • Errors are allowed to be handled or declared, but this is bad practice.

Runtime Exceptions

  • ArithmeticException Thrown by the JVM when code attempts to divide by zero
  • ArrayIndexOutOfBoundsException Thrown by the JVM when code uses an illegal index to access an array
  • ClassCastException Thrown by the JVM when an attempt is made to cast an object to a subclass of which it is not an instance
  • NullPointerException Thrown by the JVM when there is a null reference where an object is required
  • IllegalArgumentException Thrown by the programmer to indicate that a method has been passed an illegal or inappropriate argument
  • NumberFormatException Thrown by the programmer when an attempt is made to convert a string to a numeric type but the string doesn’t have an appropriate format. NumberFormatException is a subclass of IllegalArgumentException

Checked Exceptions

  • IOException Thrown programmatically when there’s a problem reading or writing a file
  • FileNotFoundException Thrown programmatically when code tries to reference a file that does not exist. FileNotFoundException is a subclass of IOException.

Errors

  • ExceptionInInitializerError Thrown by the JVM when a static initializer throws an exception and doesn’t handle it
  • StackOverflowError Thrown by the JVM when a method calls itself too many times (this is called infinite recursion because the method typically calls itself without end)
  • NoClassDefFoundError Thrown by the JVM when a class that the code uses is available at compile time but not runtime

try-catch-finally

A try statement must have a catch or a finally block.

finally is typically used to close resources such as files or databases

System.exit(0)

A rule exists for the order of the catch blocks. Java looks at them in the order they appear. If it is impossible for one of the catch blocks to be executed, a compiler error about unreachable code occurs. This happens when a superclass is caught before a subclass.

no superclass exception type appears in an earlier catch block than its subclass, otherwise, compile error.

If both catch and finally throw an exception, the one from finally gets thrown.


override

When a method overrides a method in a superclass or interface, it is not allowed to add new checked exceptions. It is allowed to declare fewer exceptions or declare a subclass of a declared exception.

it’s okay to declare new runtime exceptions in a subclass method because the declaration is redundant. Methods are free to throw any runtime exceptions they want without mentioning them in the method declaration.



作者:Leon-HB
来源链接:https://www.cnblogs.com/leon1994/p/14293960.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/124743.html

分享给朋友:

“Chapter 6 : Exceptions” 的相关文章

[C#]richtextbox实现行号2022年05月17日 20:27:54
投票统计,遍历数据2022年05月21日 11:41:42
信息系统项目文档及其管理2022年05月24日 20:53:47
信息系统合同管理2022年05月24日 22:14:31
web.xml配置web中的key points2022年05月29日 22:48:32
python常用时间处理方法2022年06月02日 20:15:25
Win 10安装Python及环境变量配置2022年06月08日 23:15:11
Linux配置Python默认版本2022年06月10日 20:31:02