package com.urlcrawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.Queue;

public class UrlFetcher {
    private URL url;
    private BufferedReader in;
    private String inputLine;
    private Queue<URL> todoUrls;
    private Integer counter;
    private Integer limit;
    private LineParserImpl lineParser;

    public void setLineParser(LineParserImpl lineParser) {
        this.lineParser = lineParser;
    }

    public boolean isFinished() {
        return todoUrls.isEmpty() || counter > limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public void setCounter(Integer counter) {
        this.counter = counter;
    }

    public void setTodoUrls(Queue<URL> todoUrls) {
        this.todoUrls = todoUrls;
    }

    public void setUrl(URL url) {
        this.url = url;
    }

    public URL getUrl() {
        return url;
    }

    public void fetchUrl() throws IOException {
        setUrl(todoUrls.remove());
        System.out.println("fetchUrl: " + getUrl());
        initBufferedReader();
        while (nextLine()) {
            List<URL> links = lineParser.extractUrl(inputLine);
            if (!links.isEmpty()) {
                todoUrls.addAll(links);
                counter += links.size();
            }
        }
        in.close();
    }

    private void initBufferedReader() throws IOException {
        in = new BufferedReader(new InputStreamReader(getUrl().openStream()));
    }

    private boolean nextLine() throws IOException {
        inputLine = in.readLine();
        return !(inputLine == null);
    }

}

@@ -1401,18 +1401,58 @@
    i
-n.close();
+f (in != null) %7B%0A            in.close();%0A        %7D
 %0A   
@@ -1506,32 +1506,50 @@
 s IOException %7B%0A
+        try %7B%0A    
         in = new
@@ -1564,16 +1564,37 @@
 dReader(
+%0A                    
 new Inpu
@@ -1640,62 +1640,258 @@
     
-%7D%0A%0A    private boolean nextLine() throws IOException %7B
+    %7D catch (IOException e) %7B%0A            in = null;%0A            System.out.println(%22Failed to load url: %22 + getUrl());%0A        %7D%0A    %7D%0A%0A    private boolean nextLine() throws IOException %7B%0A        if (in == null) %7B%0A            return false;%0A        %7D
 %0A   

Add try-catch for not available links