前段时候用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