知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

      • Theory:Errors in programs
      • Theory:What is an exception
      • Theory:NPE
      • Theory:Array exceptions
      • Theory:Hierarchy of exceptions
        • Hierarchy of exceptions
        • Checked and unchecked exceptions
        • Conclusion
      • Theory:Exception Handling
      • Theory:Throwing exceptions
      • Theory:Cutom exceptions
      • Theory:What is a bug
      • Theory:Introduction to logging
      • Theory:Debugging techniques
    • Algorithm & Data structure

    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Error & Exception
Jim
2022-08-04
目录

Theory:Hierarchy of exceptions

Java is primarily an object-oriented language. In such a paradigm, all exceptions are considered objects of special classes organized into a class hierarchy. Understanding this hierarchy is essential both for job interviews and daily programming practice.

# Hierarchy of exceptions

The following picture illustrates the simplified hierarchy of exceptions:

img

The base class for all exceptions is java.lang.Throwable. This class provides a set of common methods for all exceptions:

  • String getMessage() returns the detailed string message of this exception object;
  • Throwable getCause() returns the cause of this exception or null if the cause is nonexistent or unknown;
  • printStackTrace() prints the stack trace on the standard error stream.

We will return to the methods and constructors of this class in the following topics.

The Throwable class has two direct subclasses: java.lang.Error and java.lang.Exception.

  • subclasses of the Error class represent low-level exceptions in the JVM, for example: OutOfMemoryError, StackOverflowError;
  • subclasses of the Exception class deal with exceptional events inside applications, such as: RuntimeException, IOException;
  • the RuntimeException class is a rather special subclass of Exception. It represents so-called unchecked exceptions, including: ArithmeticException, NumberFormatException, NullPointerException.

While developing an application, you normally will process objects of the Exception class and its subclasses. We won't discuss Error and its subclasses here.

提示

The four basic classes of exceptions (Throwable, Exception, RuntimeException and Error) are located in the java.lang package. They do not need to be imported. Yet their subclasses might be placed in different packages.

# Checked and unchecked exceptions

All exceptions can be divided into two groups: checked and unchecked. They are functionally equivalent but there is a difference from the compiler's point of view.

1. Checked exceptions are represented by the Exception class, excluding the RuntimeException subclass. The compiler checks whether the programmer expects the occurrence of such exceptions in a program or not.

If a method throws a checked exception, this must be marked in the declaration using the special throws keyword. Otherwise, the program will not compile.

Let's take a look at the example. We use the Scanner class, which you are already familiar with, as a means to read from standard input, to read from a file:

public static String readLineFromFile() throws FileNotFoundException {
    Scanner scanner = new Scanner(new File("file.txt")); // java.io.FileNotFoundException
    return scanner.nextLine();
}
1
2
3
4

Here, FileNotFoundException is a standard checked exception. This constructor of Scanner declares a FileNotFoundException exception, because we assume that the specified file may not exist. Most importantly, there is a single line in the method that may throw an exception, so we put the throws keyword in the method declaration.

2. Unchecked exceptions are represented by the RuntimeException class and all its subclasses. The compiler does not check whether the programmer expects the occurrence of such exceptions in a program.

Here is a method that throws NumberFormatException when the input string has an invalid format (e.g., "abc").

public static Long convertStringToLong(String str) {
    return Long.parseLong(str); // It may throw a NumberFormatException
}
1
2
3

This code always successfully compiles without the throws keyword in the declaration.

提示

Runtime exceptions may occur anywhere in a program. The compiler doesn't require that you specify runtime exceptions in declarations. Adding them to each method's declaration would reduce the clarity of a program.

The Error class and its subclasses are also considered as unchecked exceptions. However, they form a separate class.

# Conclusion

All exceptions are represented by the Throwable class, which has two subclasses: Exception and Error. There are also two types of exceptions: checked and unchecked.

Unchecked exceptions are expected by the compiler, so you don't have to handle them. They are represented by the RuntimeException subclass of the Exception class. Errors from the Error class are also considered unchecked.

Checked exceptions have to be handled and indicated explicitly. They are located in all the other subclasses of Exception.

编辑 (opens new window)
#Java#Exception
上次更新: 2022/09/26, 16:55:15
Theory:Array exceptions
Theory:Exception Handling

← Theory:Array exceptions Theory:Exception Handling→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式