java for 跳过_在for循环中跳过错误
一種(臟)方法是使用帶有空函數(shù)的 tryCatch 進(jìn)行錯(cuò)誤處理 . 例如,以下代碼引發(fā)錯(cuò)誤并中斷循環(huán):
for (i in 1:10) {
print(i)
if (i==7) stop("Urgh, the iphone is in the blender !")
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
Erreur : Urgh, the iphone is in the blender !
但是你可以將你的指令包裝到帶有錯(cuò)誤處理函數(shù)的_1007862中,該函數(shù)不執(zhí)行任何操作,例如:
for (i in 1:10) {
tryCatch({
print(i)
if (i==7) stop("Urgh, the iphone is in the blender !")
}, error=function(e){})
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
但我認(rèn)為至少應(yīng)該打印錯(cuò)誤消息,以便在讓代碼繼續(xù)運(yùn)行時(shí)知道是否發(fā)生了錯(cuò)誤:
for (i in 1:10) {
tryCatch({
print(i)
if (i==7) stop("Urgh, the iphone is in the blender !")
}, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
ERROR : Urgh, the iphone is in the blender !
[1] 8
[1] 9
[1] 10
EDIT : 因此,在您的情況下應(yīng)用 tryCatch 將是這樣的:
for (v in 2:180){
tryCatch({
mypath=file.path("C:", "file1", (paste("graph",names(mydata[columnname]), ".pdf", sep="-")))
pdf(file=mypath)
mytitle = paste("anything")
myplotfunction(mydata[,columnnumber]) ## this function is defined previously in the program
dev.off()
}, error=function(e){cat("ERROR :",conditionMessage(e), "\n")})
}
總結(jié)
以上是生活随笔為你收集整理的java for 跳过_在for循环中跳过错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: seo多少钱啊?
- 下一篇: java signature 性能_Ja