2016-12-03 3 views
0

新に失敗構築: を(このqestionは、Javaの例外ではなく、Jsoup程度であることに注意してください)代わりにJavaのでMalformedURLExceptionを引くのため、私と一緒に負担してください、Javaへ

HTMLページを取得するためにJsoupを使用して(Jsoup.connect(current_url._name).get();)、私はJsoupのドキュメントに従って、可能な5つの例外を全てキャッチしようとしました:here

プログラムはうまく動作しますが、意図的に1 URLは何が起きているかを確認するために、私は例外がキャッチされていなかったことに驚きました。代わりにプログラムの実行が開始され、 "ビルドに失敗しました"というメッセージが表示されます。 プログラムをビルドするときだけに失敗がないので、実際には
ビルド問題ではないと思います。

// load html and check them: 
    for(URL current_url : URLs) 
    { 
     // no keyword - all getting 'yes' 
     if(keywords.isEmpty()) 
     { 
      current_url._stat = URL_stat.YES; 
     } 
     // there are keywords - get pages and check them 
     else 
     { 
      Document html_doc; 
      // try to get document and catch all errors 
      try 
      { 
       html_doc = Jsoup.connect(current_url._name).get(); 
      } 
      catch(MalformedURLException e) 
      { 
       System.out.println("the request " + current_url._name + 
            " URL is malformed"); 
       System.out.println(e.getMessage()); 
       current_url._stat = URL_stat.ERROR; 
      } 
      catch(HttpStatusException e) 
      { 
       System.out.println("page " + current_url._name + " response" 
            + " is not ok"); 
       System.out.println(e.getMessage()); 
       current_url._stat = URL_stat.ERROR; 
      } 
      catch(UnsupportedMimeTypeException e) 
      { 
       System.out.println("page " + current_url._name+ " mime type" 
            + " is not supported"); 
       System.out.println(e.getMessage()); 
       current_url._stat = URL_stat.ERROR; 
      } 
      catch(SocketTimeoutException e) 
      { 
       System.out.println("connection to " + current_url._name + 
            " times out"); 
       System.out.println(e.getMessage()); 
       current_url._stat = URL_stat.ERROR; 
      } 
      catch(IOException e) 
      { 
       System.out.println("an error occurred while getting page " 
            + current_url._name); 
       System.out.println(e.getMessage()); 
       current_url._stat = URL_stat.ERROR; 
      } 
      // check if document has paragraphs, if not mark - no 

     } 
    } 

と出力:ここ

はコードである

Exception in thread "main" java.lang.IllegalArgumentException: Malformed URL: ttp://cooking.nytimes.com/topics/what-to-cook-this-week 
at org.jsoup.helper.HttpConnection.url(HttpConnection.java:76) 
at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:36) 
at org.jsoup.Jsoup.connect(Jsoup.java:73) 
at ex2.Ex2.main(Ex2.java:123) 
Caused by: java.net.MalformedURLException: unknown protocol: ttp 
at java.net.URL.<init>(URL.java:600) 
at java.net.URL.<init>(URL.java:490) 
at java.net.URL.<init>(URL.java:439) 
at org.jsoup.helper.HttpConnection.url(HttpConnection.java:74) 
... 3 more 
C:\Users\Administrator\AppData\Local\NetBeans\Cache\8.2\executor-  snippets\run.xml:53: Java returned: 1 
BUILD FAILED (total time: 3 seconds) 

おかげで、

答えて

0

これは、最初に例外がスローされるためであるあなたが定義されていない例外:IllegalArgumentException、あります他のキャッチブロックでカスタムエラーメッセージを受け取ることができなくなります。

関連する問題