验证成功的String以在Java中枚举转换

Dim*_*ima 5 java enums exception

我有一个在Web服务实现中定义为枚举的帐户类型列表.但是,当消费者调用Web服务时,它会传递一个需要转换为枚举的String.

什么是验证给定String将成功转换为枚举的好方法?

我使用了以下方法,但这可能是滥用异常(根据Effective Java,第57项).

AccountType accountType = null;
try{
    accountType = AccountType.valueOf(accountTypeString);
}catch(IllegalArgumentException e){
    // report error
}

if (accountType != null){
    // do stuff
}else{
    // exit
}
Run Code Online (Sandbox Code Playgroud)