2
注意:我当前的解决方案正在运行(我认为)。我只是想确保我没有失去任何东西。如何检查Throwable是否是无效电子邮件地址的结果
我的问题:我想知道如何检查异常是否是由于无效的电子邮件地址造成的。使用Java邮件。
我目前正在检查SMTPAddressFailedException s,其中getAddress()和AddressException s与getRef()。
这是我目前进行检查的方法。 我错过了什么?
/**
* Checks to find an invalid address error in the given exception. Any found will be added to the ErrorController's
* list of invalid addresses. If an exception is found which does not contain an invalid address, returns false.
*
* @param exception the MessagingException which could possibly hold the invalid address
* @return if the exception is not an invalid address exception.
*/
public boolean handleEmailException(Throwable exception) {
String invalidAddress;
do {
if (exception instanceof SMTPAddressFailedException) {
SMTPAddressFailedException smtpAddressFailedException = (SMTPAddressFailedException) exception;
InternetAddress internetAddress = smtpAddressFailedException.getAddress();
invalidAddress = internetAddress.getAddress();
} else if (exception instanceof AddressException) {
AddressException addressException = (AddressException) exception;
invalidAddress = addressException.getRef();
}
//Here is where I might do a few more else ifs if there are any other applicable exceptions.
else {
return false;
}
if (invalidAddress != null) {
//Here's where I do something with the invalid address.
}
exception = exception.getCause();
} while (exception != null);
return true;
}
注:如果你很好奇(或者是有帮助),我用的是Java Helper Library发送电子邮件(见本line),因此,这就是错误最初抛出。
这是一个好点,在任何其他情况下,这是我会做的。然而,在我的特殊情况下,我宁愿使用一种方法来检查。无论如何,你会得到番石榴的'Throwable.getRootCause'的参考答案:)谢谢! – kentcdodds 2012-08-08 14:50:31