Flor Esteban
Flor Esteban

Reputation: 31

How to handle exceptions with Jsoup in java to keep program running

my program crash when user inserts a weird url

It should go like

while(condition) {
  try {
   String url = reciveURL();
   Document rss = Jsoup.connect( url ).get(); 
 } 
 catch (MalformedURLException e) {
    System.err.println("Invalid URL");
 } catch (OthersExceptions e){
    Others.Actions();
 }
}

The problem is, this throws "java.net.MalformedURLException: no protocol", instead of printing "Invalid URL" and the program crash (when user inserts any other kind of text)

Thanks!

Upvotes: 1

Views: 132

Answers (1)

Flor Esteban
Flor Esteban

Reputation: 31

Lol, solved myself, but i'll let the question as theres no post on the same issue

You should import java.net.url, this brings the "URL" type, which triggers the MalformedURLException (Jsoup doesnt do this)

So it goes like this

 while(true){
           {
             String url = reciveURL() ;
             URL chk_url = new URL(url);                 
             Document rss = Jsoup.connect( url ).get(); 
        } catch (MalformedURLException e) {
            System.err.println("url mal puesta!");
        }        
   }

Upvotes: 2

Related Questions