我有下面写的例子:
public class Test {
private enum Example
{
ONE,
TWO,
THREE,
NONE;
public Example getType(int number)
{
if (number == 1)
return ONE;
if (number == 2)
return TWO;
if (number == 3)
return THREE;
return NONE;
}
}
public void printNumber(int number)
{
Example t = Example.getType(number);
switch(t)
{
ONE:
TWO:
THREE:
System.out.println(number);
NONE:
// Do nothing
break;
default:
// Forced to put this here
throw new java.lang.UnsupportedOperationException("")
break;
}
}
}
我上面的代码的问题是默认值,该问题用于引发运行时异常,因为switch语句的输入是一个枚举:
如果我忽略了其中一个值而不是使用默认值抛出异常,是否有办法创建编译器错误?