java - code is coming in catch block even if no exception is thrown -


i tyring ensure thta contents url written file. using following code

public void copyfilefromurl(url source, file target, int count) throws ioexception {      inputstream in = null;     outputstream out = null;      if (target != null) {         try {                    if (!target.exists()) {                 target.createnewfile();                 log.debug("target file created " + target);                 log.debug("downloading source .... " + source);                  if (source == null) {                                        log.debug("null source .... " + scormfilewriter.class.getname());                     return;                          } else {                         in = source.openstream();                    }                    out = new fileoutputstream(target);                  byte[] buf = new byte[1024];                 int len;                 while ((len = in.read(buf)) > 0) {                     out.write(buf, 0, len);                 }                  log.debug("the contents url: " + source + " written file " + target);              } else {                 log.debug("skipping creation of asset");             }          } catch (exception e) {              if(count < 3){                 log.debug("trouble " + target);                 if (in != null) {                            in.close();                  }                 if (out != null) {                        out.close();                    }                  // attempt delete                 boolean success = target.delete();                  if (!success) {                      log.debug("unable delete " + target);                     } else {                     copyfilefromurl(source, target, ++count);                    }                            }              log.debug("trouble in downloading source after trying " + count +  " times: " + source);             e.printstacktrace();                 } {                      if (in != null) {                                in.close();                          }                        if (out != null) {                                out.close();                            }                      }            }    } 

now happening suppose on first calling when function comes

while ((len = in.read(buf)) > 0) {     out.write(buf, 0, len); } 

i unplug cable, exception thrown , code comes catch block , call function again. plug in cable, time while loops complete , line

log.debug("the contents url: " + source + " written file " + target); 

prints, code comes block , code comes these 2 lines

log.debug("trouble in downloading source after trying " + count +  " times: " + source); e.printstacktrace(); 

why? time no exception thrown, works fine, why code comes catch block? time after code should return normal?

thanks

you recursively calling method. first time exception thrown, code forks go , call again, printing line not reached until execution thread comes second call method. once method completes properly, execution returns first "instance" of method , execution falls through printing line. think better approach loop through attempt file instead of recursively calling same method. if must call recursively, make sure methods needs do, execution wise, before calling again.

edit
can move printing lines before recursive call method have nothing when execution comes except "unroll" recursive calls. if want avoid recursive calls thinking along lines of letting loop go on maximum of 3 times if successful exit loop, otherwise, let loop go top. along lines of:

inputstream in = null; outputstream out = null;  if (target != null) {   while(n<3){     try {                if (!target.exists()) {             target.createnewfile();             log.debug("target file created " + target);             log.debug("downloading source .... " + source);              if (source == null) {                                    log.debug("null source .... " + scormfilewriter.class.getname());                 return;                      } else {                     in = source.openstream();                }                out = new fileoutputstream(target);              byte[] buf = new byte[1024];             int len;             while ((len = in.read(buf)) > 0) {                 out.write(buf, 0, len);             }              log.debug("the contents url: " + source + " written file " + target);          } else {             log.debug("skipping creation of asset");         }         n=4; or break;     } catch (exception e) {               log.debug("trouble " + target);             if (in != null) {                        in.close();              }             if (out != null) {                    out.close();                }              // attempt delete             boolean success = target.delete();              if (!success) {                  log.debug("unable delete " + target);              } else {                // copyfilefromurl(source, target, ++count);             }                            n++;          if(n == 2){             log.debug("trouble in downloading source after trying " + count +  " times: " +           source);             e.printstacktrace();                 }     } {                  if (in != null) {                            in.close();                      }                    if (out != null) {                            out.close();                        }                  }          } 

of course may need adjust logging , exit conditions meet specific needs


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -