R和python中试错运行语句
R中试错运行语句
采用tryCatch({ error = function(e) { } }
for (i in 1:n_subj) { #n_subj Total number of subjects (0D)
subj <- subjs[i] #subjs List of unique subjects (1D)
DT_subj <- raw_data[subjID == subj]
blocks_of_subj <- unique(DT_subj$block_name)
#subjIndex[i] = subj
#print(subj)
for (b in 1:b_subjs[i]) { #b_subjs, Number of blocks per each subject
curr_block <- blocks_of_subj[b]
DT_curr_block <- DT_subj[block_name == curr_block]
#print(curr_block)
#t <- t_subjs[i, b]
#blcokIndex[i*b] = curr_block
for (c in 1:feedback_num) { #feedback_num, Number of feedback
curr_feedback = feedback[c]
feedback_index = DT_curr_block[feedback_type == curr_feedback,
which = TRUE]
nextRESP_index = feedback_index + 1
temp_dishonesty = tryCatch({
if (tail(nextRESP_index, n = 1) > t_max){ #如果超过t_max界限,去掉最后一个
nextRESP_index = head(nextRESP_index, -1)
}
temp = DT_curr_block[nextRESP_index, ]
nrow(temp[whether_dishonesty == 1])/nrow(temp) #计算出不同feedback后下一轮选择欺骗的比例
},
error = function(e) {
#print(paste("subid: ", subj))
#print(paste("block: ", curr_block))
return(NA)
})
subjIndex = c(subjIndex, subj)
blcokIndex = c(blcokIndex, curr_block)
feedbackIndex = c(feedbackIndex, curr_feedback)
dishonesty_ratio = c(dishonesty_ratio, temp_dishonesty)
}
}
}
Python中试错运行语句
try:
import numpy as np # Numeric calculation
import pandas as pd # General purpose data analysis library
import squeak # For mouse data
except:
raise Exception("\
Whoops, you're missing some of the dependencies you need to run this script.\n\
You need to have numpy, pandas, and squeak installed.")
Javascript中试错运行语句
[JavaScript 教程]https://developer.mozilla.org/zh-CN/docs/Web/JavaScript
JavaScript ( JS ) 是一种具有 函数优先的轻量级,解释型或即时编译型的编程语言。虽然它是作为开发Web 页面的脚本语言而出名的,但是它也被用到了很多 非浏览器环境中,例如 Node.js、 Apache CouchDB 和 Adobe Acrobat。JavaScript 是一种 基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式和声明式(如函数式编程)风格。了解更多 JavaScript。
**try...catch
**语句标记要尝试的语句块,并指定一个出现异常时抛出的响应。
try {
nonExistentFunction();
} catch (error) {
console.error(error);
// expected output: ReferenceError: nonExistentFunction is not defined
// Note - error messages will vary depending on browser
}