我有一个脚本,它将一组记录发送到一个文件中。我正在使用 Try - Catch 块来处理异常。在 catch 块中,我有一个代码,其中包含指向下一条记录的指针。但这不是执行。基本上我想跳过坏记录 n 移动到下一个记录。
while(currentrecord)
{
try
{
writerecord event
}
catch
{
currentrecord = next record
}
}
我有一个脚本,它将一组记录发送到一个文件中。我正在使用 Try - Catch 块来处理异常。在 catch 块中,我有一个代码,其中包含指向下一条记录的指针。但这不是执行。基本上我想跳过坏记录 n 移动到下一个记录。
while(currentrecord)
{
try
{
writerecord event
}
catch
{
currentrecord = next record
}
}
在大多数语言中(除非您使用非常奇怪的东西),如果“writerecord event”没有引发异常,则不会调用 catch 块。
你不是说:
while(currentrecord) {
try { writerecord event }
catch { log error }
finally { currentrecord = next record}
}
您是否尝试遍历查询返回的一些记录?做这样的事情:
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());
}
您可以使用 try-finally 结构,这样无论代码是否抛出异常,都会始终执行 finally 块中的任何内容。它通常用于清理资源,例如关闭文件或连接。如果没有 catch 子句,try 块中的任何抛出异常都将中止执行,跳转到 finally 块并运行该代码。
同意“终于”可能是这里最好的选择——但我们知道实际的例外是什么吗?- 你能在你的catch循环中输出它吗,这样:
A)您可以证明正在抛出异常(而不是说返回“null”或其他东西)
B)确保你得到的异常不会阻止'nextrecord'正常工作...... [不确定'finally'在这种情况下会实现什么 - 大概异常必须冒泡到调用代码?
因此,如果您未能提交此记录,您将尝试进入下一条记录。罗伯特穆勒是对的。解释...
如果 WriteRecord 失败,则业务组件仍将定位在脏记录上。尝试移动到下一条记录将使 buscomp 尝试再次写入它——因为有一个称为“隐式保存”的功能。
解决方案:您必须撤消记录 (UndoRecord) 以放弃失败的字段更改,然后再进行下一个更改。