Results 1 to 2 of 2
  1. #1
    nondescript is offline Member
    Join Date
    Jul 2014
    Rep Power

    Default Why is exception not thrown when the connection is terminated?

    hello everyone,

    I am here to solve an issue i am facing. I hope to get some help from the experts.

    Below is a code of a simple file downloader in java. I want to detect an exception when the connection is broken in the middle of downloading a file. Now, I run the code and in the middle of downloading the file, i turn off my wifi with keyboard wifi off button. Doing this the program hangs forever without throwing any exceptions and without terminating. It seems it blocks forever. Firstly my confusion is why is exception not thrown? Secondly, in the following code you can see this line //con.setReadTimeout(2000); which is currently commented out. Removing the comment and running the program, now if connection breaks in the middle by turning off wifi then it waits for 2 seconds and then if it cannot read then it terminates without throwing exception. So, again in this case why is it just terminating and not throwing any exception? I am very puzzled by this behavior. I hope i can get some help. Thank you very much!

    Java Code:
    import java.nio.channels.Channels;
    import java.nio.channels.ReadableByteChannel;
    import java.util.*;
    	public class Main {
    	    public static void main(String[] args) {
    	    		URL website = new URL("");
    	    		URLConnection con = website.openConnection();
    		    	ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
    		    	FileOutputStream fos = new FileOutputStream("song.mp3");
    		    	fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    	    	catch(Exception e){
    	    		System.out.println("got here");
    	    	System.out.println("Done downloading...");

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Why is exception not thrown when the connection is terminated?

    I can't be certain but I would suspect that a down interface is not sufficient to generate an exception since its state is non-deterministic. So it could come back up and the transfer would continue. You might be able to use the setReadTimeout() method as you tried and compare the amount transferred vs the size of the target file and then take other action such as retry or terminate the task. The size can be obtained via
    the URLconnection.getContentLength() instance method.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 5
    Last Post: 05-11-2014, 09:42 PM
  2. wrongly thrown exception
    By dawnMist in forum New To Java
    Replies: 1
    Last Post: 11-29-2012, 02:24 PM
  3. Replies: 5
    Last Post: 02-15-2011, 12:39 AM
  4. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 03:29 PM
  5. Which exception is thrown.....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 04:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts