java for 跳过_在for循环中跳过错误
一種(臟)方法是使用帶有空函數的 tryCatch 進行錯誤處理 . 例如,以下代碼引發錯誤并中斷循環:
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 !
但是你可以將你的指令包裝到帶有錯誤處理函數的_1007862中,該函數不執行任何操作,例如:
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
但我認為至少應該打印錯誤消息,以便在讓代碼繼續運行時知道是否發生了錯誤:
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 : 因此,在您的情況下應用 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")})
}
總結
以上是生活随笔為你收集整理的java for 跳过_在for循环中跳过错误的全部內容,希望文章能夠幫你解決所遇到的問題。