Jsoup UncheckedIOException

 前段时候用jsoup 1.11.1解析爬虫网站时,发现执行过程中会抛出UncheckedIOException,即使用try catch中用Exception也捕获失败。看了下jsoup的源码,这玩样直接继承了ERROR,好家伙,这谁顶得住

package org.jsoup;

import java.io.IOException;

public class UncheckedIOException extends Error {
    public UncheckedIOException(IOException cause) {
        super(cause);
    }

    public IOException ioException() {
        return (IOException) getCause();
    }
}

源码里直接捕获IOException 抛出 UncheckedException

 private void prepareByteData() {
            Validate.isTrue(executed, "Request must be executed (with .execute(), .get(), or .post() before getting response body");
            if (byteData == null) {
                Validate.isFalse(inputStreamRead, "Request has already been read (with .parse())");
                try {
                    byteData = DataUtil.readToByteBuffer(bodyStream, req.maxBodySize());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                } finally {
                    inputStreamRead = true;
                    safeClose();
                }
            }
        }

看了下1.12.1版本的Jsoup源码,好家伙,终于意识到问题了吗,直接改成了继承自RuntimeException

package org.jsoup;

import java.io.IOException;

public class UncheckedIOException extends RuntimeException {
    public UncheckedIOException(IOException cause) {
        super(cause);
    }

    public UncheckedIOException(String message) {
        super(new IOException(message));
    }

    public IOException ioException() {
        return (IOException) getCause();
    }
}

解决方案1:在catch中加入UncheckIOException(推荐)或者直接捕获Error即可

解决方案2:直接升级成1.12.1版本以上,别用1.11.1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值