由于网络问题经常会出现io异常,这时需要重复请求,并且设定一个异常重复请求次数n,当重复请求到第n次时再抛出io异常,这种问题不知道各位大侠是怎么处理的,有什么好办法?这是我的方法        for (int i=0;i<10;i++){//请求失败重复请求
try {
doc = Jsoup.parse(url, this.getTimeoutMills());
i=10;
} catch (IOException e) {
if(i==10-1){
throw e;
}
}
        }

解决方案 »

  1.   


        public static void g(int n) throws IOException {
            List<IOException> list = null;
            while (n > 0) {
                try {
                    doc = Jsoup.parse(url, this.getTimeoutMills());
                    return;
                } catch (IOException io) {
                    if (list == null) {
                        list = new ArrayList<IOException>(n);
                    }
                    list.add(io);
                    n--;
                }
            }
            ManyIOException ex = new ManyIOException();
            ex.setList(list);
            throw ex;
        }    public static class ManyIOException extends IOException {        public ManyIOException(String message) {
            }        public ManyIOException() {
            }
            private List<IOException> list;        public List<IOException> getList() {
                return list;
            }        public void setList(List<IOException> list) {
                this.list = list;
            }
        }
      

  2.   

    catch里写
    if(i!=10){ continue;}
    else{throw e}