gpt4 book ai didi

java - 即使没有抛出异常,代码也会进入 catch block

转载 作者:行者123 更新时间:2023-11-30 04:22:57 25 4
gpt4 key购买 nike

我正在努力确保 url 中的内容成功写入文件。为此,我使用以下代码

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 for " + 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 from the URL: " + source + " are successfully written to the file " + target);

} else {
log.debug("skipping creation of asset");
}

} catch (Exception e) {

if(count < 3){
log.debug("trouble with " + target);
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}

// Attempt to delete it
boolean success = target.delete();

if (!success) {
log.debug("Unable to delete " + target);
} else {
copyFileFromUrl(source, target, ++count);
}
}

log.debug("trouble in downloading source after trying " + count + " times: " + source);
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

现在假设函数到达时第一次调用时发生了什么

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

我拔掉电缆,抛出异常,代码进入 catch block 并再次调用该函数。现在我插入电缆,这次 while 循环完成,线路

log.debug("The contents from the URL: " + source + " are successfully written to the file " + target);

打印,代码来到finally block 然后代码来到这两行

log.debug("trouble in downloading source after trying " + count +  " times: " + source);
e.printStackTrace();
为什么?这次没有抛出异常,一切正常,为什么代码会出现catch block ?这次finally之后代码应该恢复正常了吧?

谢谢

最佳答案

您正在递归调用该方法。第一次引发异常时,代码会 fork 并再次调用自身,直到执行线程从该方法的第二次调用返回后才会到达打印行。一旦该方法正确完成,执行将返回到该方法的第一个“实例”,并且执行将继续到打印行。我认为更好的方法是循环尝试获取文件,而不是递归调用相同的方法。如果必须递归调用它,请确保这些方法在再次调用自身之前完成了它需要执行的所有操作。

编辑
您始终可以将打印行移到递归调用之前,这样当执行返回到该方法时,除了“展开”递归调用之外,该方法将不执行任何操作。如果你想避免递归调用,我的想法是让循环最多进行 3 次,但如果成功,你将退出循环,否则,就让循环回到顶部。大致如下:

InputStream in = null;
OutputStream out = null;

if (target != null) {
while(n<3){
try {
if (!target.exists()) {
target.createNewFile();
log.debug("target file created for " + 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 from the URL: " + source + " are successfully written to the file " + target);

} else {
log.debug("skipping creation of asset");
}
n=4; or break;
} catch (Exception e) {


log.debug("trouble with " + target);
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}

// Attempt to delete it
boolean success = target.delete();

if (!success) {
log.debug("Unable to delete " + target);

} else {
// copyFileFromUrl(source, target, ++count);
}

n++;
if(n == 2){
log.debug("trouble in downloading source after trying " + count + " times: " + source);
e.printStackTrace();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}

当然,您可能需要调整日志记录和退出条件以满足您的特定需求

关于java - 即使没有抛出异常,代码也会进入 catch block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16560899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com