首页 > 其他 > 详细

Chapter 6 : Exceptions

时间:2021-01-18 19:49:47      阅读:33      评论:0      收藏:0      [点我收藏+]

技术分享图片

  • 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

技术分享图片

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.

Chapter 6 : Exceptions

原文:https://www.cnblogs.com/leon1994/p/14293960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!