本文介绍了尝试重复插入密钥时,正确的异常会被抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复地,我看到有关避免抛出通用的$ code RuntimeException 的注释,我正在努力遵循该准则。

Repeatedly I see comments about avoiding throwing generic RuntimeException and I am trying to follow that guideline.

我有一个类汇总一个 SortedMap 与一个属性设置允许或不允许重复的键。我试图找出当不允许重复的键时,我应该抛出什么异常,并尝试添加一个。

I have a class that aggregates a SortedMap with a property setting to allow or disallow duplicate keys. I am trying to figure out what Exception I should throw when duplicate keys are disallowed and an attempt is made to add one.

我检查了Java文档中的 Exception 类,没有一个已知的直接后代似乎是合适的。我是否开始创建我自己的 EDuplicateMapKey 类,并抛出?如果是这样,如何避免最后一大堆的类文件,一个针对每个定制的异常类型?

I checked the Java docs for the Exception class and none of the known direct descendants seemed suitable. Do I just go ahead and create my own EDuplicateMapKey class for example and throw that? If so, how do I avoid ending up with a big pile of class files, one for each custom Exception type?

这里被认为是最佳实践?

What is considered "best practice" here?

推荐答案

创建你自己的例外。例如,Java EE具有,您可以为自定义地图做类似的事情。

Create your own exception. For example Java EE has DuplicateKeyException, you can do something similar as that for your custom map.

这篇关于尝试重复插入密钥时,正确的异常会被抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:42