java - 我得到 InvocationTargetException 并且我不知道如何知道真正的错误?
问题描述
我有一个类,它有一个使用 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 语句体中抛出”
我该如何处理并找到导致异常的原因?请帮忙 。
解决方案
编译器告诉您,您有代码尝试处理 InvocationTargetException,它可以告诉它永远不会被抛出,所以它是多余的,并且可以在不改变程序行为的情况下安全地删除。
删除此 catch 块并重试。
推荐阅读
- javascript - 服务器运行时的Node.js javascript命令行?
- python - 如果数据类型是“时间戳”,我们可以过滤掉 z-score <-3 或 >3 的记录吗?
- node.js - 出现错误:表达式不可调用 Mongoose NextJS
- python - 如何将模糊匹配应用于数据框中的列
- javascript - 如何让这两个组件出现在同一行?(文本框标签和文本框)
- r - R:glm 中的起始值到底是什么以及如何指定?
- javascript - 在响应 onclick 中重定向到 index.html 文件
- typescript - TypeScript:如何使用变量推断类型在类之外定义另一个具有相同类型的变量
- c++ - 如何从 C++ 头文件中导出函数
- html - 如何将 html 表格行中的数据发送到后端