为什么不是默认的 switch 语句中的每个 case 都应该涵盖异常情况?

Why should each case in a switch statement that isn't the default cover an exceptional condition?

我关于Java的书似乎暗示in switch statements each case that isn't the default should cover an exceptional condition.这是什么意思,为什么推荐?你能提供任何例子来说明这一点吗?

编辑: 我想我得到了原始引用的 meaning/implication:不符合您最初期望的输入或情况几乎总是需要涵盖,而 switch 语句中的 default case 提供了一种方法。

我想你误解了作者的意思。他说:

Including a default case focuses you on the need to process exceptional conditions.

他的意思是,你要把你所有的东西都添加到开关盒中,然后添加一个默认值将保证你会考虑所有其他可能性。例如,如果您切换为动物 "cat"、"dog"、"pig",那么,如果用户输入其他内容怎么办?添加默认值会迫使您考虑这一点,并适当地处理错误和异常情况。