无法捕获异常 - 带有HtmlUnit的Java?

时间:2011-08-03 20:02:42

标签: java exception try-catch htmlunit

我不明白发生了什么???

我有以下代码:

public static void main(String[] args){ 

    WebClient wc = null;
    HtmlPage hp = null;
    ArrayList<Product> allProducts;
    try{
        wc = new WebClient(BrowserVersion.FIREFOX_3_6);
        hp = wc.getPage("http://www.mega.co.il/");
    }catch (Exception e) {

    }
    finally{
        allProducts = getProducts(wc, hp);
        for (Product product : allProducts) {
            System.out.println(product);
        }
    }
}

并且控制台仍显示抛出的javax.net.ssl.SSLPeerUnverifiedException hp = wc.getPage(“http://www.mega.co.il/”); line:

    Aug 3, 2011 10:45:19 PM com.gargoylesoftware.htmlunit.html.BaseFrame loadInnerPageIfPossible
SEVERE: IOException when getting content for iframe: url=[http://www.mega.co.il/jsfweb/logins/rightLoginHPIframe.jsf]
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(Unknown Source)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:339)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:597)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:133)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1405)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1459)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1324)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:303)
    at com.gargoylesoftware.htmlunit.html.BaseFrame.loadInnerPageIfPossible(BaseFrame.java:141)
    at com.gargoylesoftware.htmlunit.html.BaseFrame.loadInnerPage(BaseFrame.java:100)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1763)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:204)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:436)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:307)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:369)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:354)
    at DBSniffer.main(DBSniffer.java:107)

有人能解释我发生了什么吗?

P.S
不知怎的,应用程序继续运行!?

提前致谢, 丹

1 个答案:

答案 0 :(得分:4)

打印的异常永远不会传播到catch子句。它在内部捕获,并以标准错误打印。

要取消警告,您需要告诉API不要打印它们。

尝试以下方法:

webClient.setThrowExceptionOnScriptError(false);
webClient.setPrintContentOnFailingStatusCode(false);
webClient.setThrowExceptionOnScriptError(false);