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