我有一个脚本,它将一组记录发送到一个文件中。我正在使用Try-Catch块来处理异常。在catch块中,我有一个代码,它有指向下一条记录的指针。但这并没有执行。基本上我想跳过坏记录n移动到下一个记录。
while(currentrecord)
{
try
{
writerecord event
}
catch
{
currentrecord = next record
}
}
答案 0 :(得分:4)
在大多数语言中(除非你使用非常奇怪的东西),如果'writerecord event'没有抛出异常,则不会调用catch块。
你的意思是:
while(currentrecord) {
try { writerecord event }
catch { log error }
finally { currentrecord = next record}
}
答案 1 :(得分:2)
您是否尝试遍历查询返回的某些记录?做这样的事情:
var yourBusObject = TheApplication().GetBusObject("Your Business Object Name");
var yourBusComp = yourBusObject.GetBusComp("Your Business Component Name");
// activate fields that you need to access or update here
yourBusComp.ClearToQuery();
// set search specs here
yourBusComp.ExecuteQuery(ForwardOnly);
if (yourBusComp.FirstRecord()) {
do {
try {
// update the fields here
yourBusComp.WriteRecord();
} catch (e) {
// undo any changes so we can go to the next record
// If you don't do this I believe NextRecord() will implicitly save and trigger the exception again.
yourBusComp.UndoRecord();
// maybe log the error here, or just ignore it
}
} while (yourBusComp.NextRecord());
}
答案 2 :(得分:1)
您可以使用try-finally结构,以便始终执行finally块内的任何内容,无论代码是否抛出异常。它通常用于清理资源,如关闭文件或连接。如果没有catch子句,try块中的任何抛出异常都将中止执行,跳转到finally块并运行该代码。
答案 3 :(得分:0)
同意'finally'可能是最好的选择 - 但我们知道实际上是什么异常吗? - 你可以在你的catch循环中输出它,以便:
A)你可以证明一个异常被抛出(而不是说'null'被返回或者其他东西)
B)确保你得到的例外并不能阻止'nextrecord'正常工作...... [不确定'最终'会在这种情况下实现什么 - 可能是例外情况不得不冒泡到调用代码?
答案 4 :(得分:0)
如果您未能提交此记录,那么您正试图进入下一条记录。罗伯特穆勒做对了。解释......
如果WriteRecord失败,那么业务组件仍将位于脏记录上。尝试移动到下一条记录将使buscomp再次尝试写入 - 因为一个名为“隐式保存”的功能。
解决方案:您必须撤消记录(UndoRecord)以放弃失败的字段更改,然后再转到下一个字段。