在Java中创建异常的一般经验法则是什么? [英] What is the general rule of thumbs for creating an Exception in Java?

查看:64
本文介绍了在Java中创建异常的一般经验法则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过两种情况:


  • 创建过多的自定义异常

  • 也使用许多常规的Exception类

在这两种情况下,项目都开始正常,但很快就成为维护(和重构)的开销。

In both cases the project started OK but soon became an overhead to maintain (and refactor).

那么关于创建自己的异常类的最佳实践是什么?

So what is the best practice regarding the creation of your own Exception classes?

推荐答案

Java专家写了一篇有关 Java中的异常,其中列出了一些创建异常的最佳实践,总结如下:

The Java Specialists wrote a post about Exceptions in Java, and in it they list a few "best practices" for creating Exceptions, summarized below:


  • 不要编写自己的异常(有很多有用的异常已经成为Java API的一部分了)

  • Don't Write Own Exceptions (there are lots of useful Exceptions that are already part of the Java API)

编写有用的异常(如果您必须编写自己的异常,请确保它们提供有关以下问题的有用信息:

Write Useful Exceptions (if you have to write your own Exceptions, make sure they provide useful information about the problem that occurred)

这篇关于在Java中创建异常的一般经验法则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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