最后的default条件是一种防御方案。 这条语句应该或者进行相应的操作,或者包含解释为何不这样做的的注释。

如下代码演示了这个规则:

switch (state) {                       // Non-Compliant - must have a default case
  case 0:
  case 1:
    System.out.println("0 or 1!");
    break;
}

switch (state) {
  default:                             // Non-Compliant - must be last for better readability
    throw new IllegalStateException();
  case 0:
  case 1:
    System.out.println("0 or 1!");
    break;
}

switch (state) {
  case 0:
  case 1:
    System.out.println("0 or 1!");
    break;
  default:                             // Compliant
    throw new IllegalStateException();