【问题标题】:I get InvocationTargetException and I don't know how to know real error?我得到 InvocationTargetException 并且我不知道如何知道真正的错误?
【发布时间】:2018-09-24 18:17:45
【问题描述】:

我有一个类,它有一个使用 javax.mail 发送电子邮件的方法。我已经编译了代码并且工作正常,然后突然我开始收到 InvocationTargetException,我不知道是什么原因造成的。

这是我的代码:

    import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmailClass {

    private static String USER_NAME = "email@domain.com"  ; 
    private static String PASSWORD = "Password";  
    private static String RECIPIENT = "email@domain.com";

 private SendEmailClass () {

    }




    public static String getStr (String str)   {


        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Test Email";
        String body = "mail notification  " + str ;

        SendEmail(from, pass, to, subject, body);



return "test" + str ;

    }



 public static void SendEmail(String from, String pass, String[] to, String subject, String body)  {


        Properties props = System.getProperties();
        String host = "Some IP Address";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);




        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();


        }

        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }



    }






}

当我调用方法“getStr”时,我得到了那个异常,我试图通过在“SendEmail”方法的末尾放置 catch 块来处理它,所以方法变成了这样:

public static void SendEmail(String from, String pass, String[] to, String subject, String body)  {


        Properties props = System.getProperties();
        String host = "40.101.62.34";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);




        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();


        }

        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }

        catch (InvocationTargetException ex){
            // handle exception
        }

    }






}

但后来我得到了:“InvocationTargetException 的无法到达的 catch 块。这个异常永远不会从 try 语句体中抛出”

我该如何处理并找出导致异常的原因? 请帮忙。

【问题讨论】:

  • 这意味着你应该删除这个:catch (InvocationTargetException ex){ // 处理异常 } 因为这永远不会是这种情况
  • 是的,但是当我删除它时,我会得到 InvocationTargetException。我想知道真正的错误是什么。它怎么说它不能被抛出?!我很困惑。
  • 你能显示更多你的堆栈跟踪吗?此外,AddressException 和/或 MessageException 可能是 InvocationTargetException 的子类,这可能会导致这种情况。
  • 也可能是您使用了两种不同类型的 InvocationTargetException(不同的包)
  • 显示堆栈跟踪,包括指向代码行的指针。

标签: java jakarta-mail invocationtargetexception


【解决方案1】:

编译器告诉你,你有代码试图处理 InvocationTargetException 它可以告诉永远不会被抛出,所以它是多余的,可以安全地删除而不改变你的程序的行为.

删除此 catch 块并重试。

【讨论】:

  • 您可能想阅读 cmets。这也是我的第一个猜测,但他回复说,当他删除它时,会抛出 InvocationTargetException。
猜你喜欢
  • 2022-12-07
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
相关资源
最近更新 更多