知识库 知识库
首页
  • 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
      • Theory:Exception Handling
      • Theory:Throwing exceptions
      • Theory:Cutom exceptions
        • How to create and throw a custom exception
        • Best practices for custom exceptions
        • Conclusion
      • 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-06
目录

Theory:Cutom exceptions

Java exceptions cover a lot of standard exceptions we have to deal with in programming. However, sometimes we might need to create them ourselves. One of the main reasons why is to handle logical exceptions which are specific to your program. Imagine you are implementing a client-server application. The server processes user information and validates its correctness, or throws an exception. An essential condition for the server to work properly is that it should validate all the fields with user data and not stop at the first incorrect one. If some fields turn out to be incorrect, we want to receive a detailed report about what went wrong. In this case, a standard exception like IllegalArgumentException will not be enough for us, which means it's time to create a custom exception.

# How to create and throw a custom exception

To create a custom exception you need to extend the Exception (checked) or RuntimeException (unchecked) classes.

Here is an example:

public class MyAppException extends Exception {

    public MyAppException(String msg) {
        super(msg);
    }

    public MyAppException(Exception cause) {
        super(cause);
    }
}
1
2
3
4
5
6
7
8
9
10

In the example above, a new class of exceptions is declared. It is a checked exception because it extends the Exception class. The declared class has two constructors for creating instances, and they call the corresponding constructor of the base class.

Now, we can throw an instance of the class:

public static void someMethod() throws MyAppException {
    throw new MyAppException("Something bad");
}
1
2
3

For more information about throwing exceptions, please refer to our topic (opens new window) on the subject.

Now let's learn some rules of creating custom exceptions.

# Best practices for custom exceptions

First things first, make sure that your application will benefit from creating a custom exception. Otherwise, use standard Java exceptions.

Secondly, follow the naming convention — end the class name with “Exception”, for example MyAppException.

Also, provide the constructor that sets a cause in case your program catches a standard exception before throwing a custom one.

For example, let's look at the code snippet below. Here we capture the root cause of the exception with the Throwable argument, which is passed to the parent class constructor.

public class CustomException extends Exception {

   public CustomException(String message, Throwable cause) {

       super(message, cause);

   }

}
1
2
3
4
5
6
7
8
9

Is creating a custom exception always a good idea? Although the custom exception feature greatly enhances the error handling mechanism, its use is not always justified. We advise you to use standard exceptions whenever possible for a number of reasons, such as:

  • Standard exceptions are widely known by other programmers. One can understand the type of problem just by looking at the name of the exception.
  • By opting for standard exceptions, you follow the reusability principle. It makes your code clearer and more professional.

# Conclusion

Custom exceptions are a great tool for handling inconsistencies in your program. In this topic, we learned how to create and throw them, along with some best practices to follow. However, we highly recommend creating a custom exception only when it is justified. Standard Java exceptions are often a safer and no less efficient choice.

编辑 (opens new window)
#Java#Exception
上次更新: 2022/09/26, 16:55:15
Theory:Throwing exceptions
Theory:What is a bug

← Theory:Throwing exceptions Theory:What is a bug→

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