我使用Android JavaMail-API发送电子邮件。一切都按预期工作,但现在我正在测试一些东西,当我有一个糟糕的互联网连接。我这样做是通过禁用WIFI然后单击发送电子邮件按钮。
在我的SendMail方法中,我有以下内容:
try{
if(javaMailAPI.send()){
// Do something on success
}
else{
// Do something on fail
}
}
catch(UnknownHostException ex){
// Do something when I have no Internet Connection
}
catch(Exception ex){
// Do something else when I have an other Exception
}
所以,当我在断开WIFI时测试上面的内容时,这就是我的期望:
结果是什么:
07-02 15:00:47.699:W / System.err(6680):javax.mail.MessagingException: 未知的SMTP主机:smtp.gmail.com; 07-02 15:00:47.699: W / System.err(6680):嵌套异常是:07-02 15:00:47.699: W / System.err(6680):java.net.UnknownHostException:主机是 未解决:smtp.gmail.com 07-02 15:00:47.699:W / System.err(6680):at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1389) 07-02 15:00:47.709:W / System.err(6680):at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 07-02 15:00:47.709:W / System.err(6680):at javax.mail.Service.connect(Service.java:310)07-02 15:00:47.709: W / System.err(6680):at javax.mail.Service.connect(Service.java:169) 07-02 15:00:47.709:W / System.err(6680):at javax.mail.Service.connect(Service.java:118)07-02 15:00:47.709: W / System.err(6680):at javax.mail.Transport.send0(Transport.java:188) 07-02 15:00:47.709:W / System.err(6680):at javax.mail.Transport.send(Transport.java:118)07-02 15:00:47.719: W / System.err(6680):at external_software.JavaMailAPI.send(JavaMailAPI.java:118)07-02 15:00:47.719:W / System.err(6680):at controllers.EMailSender.SendEmails(EMailSender.java:58)07-02 15:00:47.719:W / System.err(6680):at activities.ChecklistResultActivity $ 3.run(ChecklistResultActivity.java:300) 07-02 15:00:47.719:W / System.err(6680):at java.lang.Thread.run(Thread.java:841)07-02 15:00:47.719: W / System.err(6680):引起:java.net.UnknownHostException:Host is 未解决:smtp.gmail.com 07-02 15:00:47.729:W / System.err(6680):at java.net.Socket.connect(Socket.java:826)07-02 15:00:47.729: W / System.err(6680):at java.net.Socket.connect(Socket.java:786)07-02 15:00:47.729:W / System.err(6680):at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233) 07-02 15:00:47.729:W / System.err(6680):at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163) 07-02 15:00:47.729:W / System.err(6680):at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359) 07-02 15:00:47.729:W / System.err(6680):... 10 more
使用其他catch中的代码。
正如您所看到的,它捕获了JavaMail-API的MessagingException,它正在嵌套UnknownHostException。
那么,我怎样才能捕获嵌套UnknownHostException的UnknownHostException / MessagingException?是否有可能以某种方式检索嵌套的Exception?
我应该使用它(source):
...
catch(Exception ex){
if(ex.getCause() != null && ex.getCause().getCause() instanceof UnknownHostException){
throw new UnknownHostException(ex.getMessage());
}
else{
// Do something else when I have an other Exception
}
}
PS:当我像上面那样抛出UnknownHostException时,是否会转到此捕获异常上方的UnknownHostException?
答案 0 :(得分:1)
我改变了这个:
try{
if(javaMailAPI.send()){
// Do something on success
}
else{
// Do something on fail
}
}
catch(UnknownHostException ex){
// Do something when I have no Internet Connection
}
catch(Exception ex){
// Do something else when I have an other Exception
}
到此:
try{
if(javaMailAPI.send()){
// Do something on success
}
else{
// Do something on fail
}
}
catch(UnknownHostException ex){
onNoConnection();
}
catch(Exception ex){
// Check if the thrown Exception is a MessagingException with a nested UnknownHostException
if(ex instanceof MessagingException){
if(((MessagingException)ex).getNextException() instanceof UnknownHostException){
onNoConnection();
}
}
else{
// Do something else when I have an other Exception
}
}
private void onNoConnection(){
// Do something when I have no Internet Connection
}
它按预期工作。