免责声明:我很久没有使用java了。
我创建这个单例类是为了创建一个httpurlconnection。
public class EstablishConnection {
HttpURLConnection conn = setupConn("some url");
private static final EstablishConnection instance = new EstablishConnection();
//private constructor to avoid client applications to use constructor
private EstablishConnection(){}
public static EstablishConnection getInstance(){
return instance;
}
private static HttpURLConnection setupConn(String addr) throws MalformedURLException, IOException, ProtocolException {
URL url = new URL(addr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
return conn;
}
}
为什么我会得到一个未报告的报告 MalformedURLException
当我把它扔进去的时候例外 setupConn
?
| HttpURLConnection conn = setupConn("some url");
unreported exception java.net.MalformedURLException; must be caught or declared to be thrown
1条答案
按热度按时间x0fgdtte1#
您正在从setupconn函数引发异常,但在此处调用时未处理引发的异常:
您可以在setupconn函数中使用try catch来处理异常,也可以在构造函数中调用该函数,然后从那里重试。