Hello everyone,
I am working with X++ in Dynamics 365 Finance & Operations and need help with error handling. I have a block of code where I’m performing multiple operations inside a while
loop, and each operation might throw an error. I want to catch all errors with a single catch
block and print the error message that is returned from the try
block.
Here is my current code: (By the way, the table can contain more than 6 million records.)
try
{
while select tmpItemTable
{
str itemBarCodeStr;
str SectorfamilyCode;
str Sectorfamily;
str EcoResCategoryCode;
real PxVenteStandard;
real PxTarif;
real PxAchatNet;
try
{
itemBarCodeStr = strFmt("%1", tmpItemTable.ItemBarCode);
}
catch
{
error("Error in Operation 1 of item file");
throw Exception::error;
}
try
{
SectorfamilyCode = subStr(tmpItemTable.SectorfamilyCode, 1, 2);
}
catch
{
error("Error in Operation 2 of item file");
throw Exception::error;
}
try
{
Sectorfamily = subStr(tmpItemTable.Sectorfamily, 1, 50);
}
catch
{
error("Error in Operation 3 of item file");
throw Exception::error;
}
try
{
EcoResCategoryCode = subStr(tmpItemTable.EcoResCategoryCode, 1, 6);
}
catch
{
error("Error in Operation 4 of item file");
throw Exception::error;
}
try
{
PxVenteStandard = roundDowndec(tmpItemTable.PxVenteStandard, 2);
}
catch
{
error("Error in Operation 5 of item file");
throw Exception::error;
}
try
{
PxTarif = roundDowndec(tmpItemTable.PxTarif, 2);
}
catch
{
error("Error in Operation 6 of item file");
throw Exception::error;
}
try
{
PxAchatNet = roundDowndec(tmpItemTable.PxAchatNet, 2);
}
catch
{
error("Error in Operation 7 of item file");
throw Exception::error;
}
try
{
io.writeExp([
parameters.Code_Soc,
tmpItemTable.Brand,
tmpItemTable.ItemName,
tmpItemTable.designation,
tmpItemTable.produitcompose,
tmpItemTable.gereenstock,
sectorfamilyCode,
sectorfamily,
tmpItemTable.ecorescategorycodeparent,
tmpItemTable.ecorescategorynameparent,
EcoResCategoryCode,
tmpItemTable.ecorescategoryname,
"",
tmpItemTable.ecorescategoryremisename,
"",
PxVenteStandard,
"",
"n",
"",
itemBarCodeStr,
PxTarif,
"",
PxAchatNet,
"",
"",
"",
tmpItemTable.typearticle,
tmpItemTable.SCAStockStrategy
]);
}
catch
{
error("Error in io.writeExp of item file");
throw Exception::error;
}
}
}
catch
{
error(“Error in while loop tmpItemTable”);
throw Exception::error;
}
Issue:
I don’t know, but I always face the issue where the code hits this error: ‘Error in io.writeExp of item file’. Since I haven’t found a way to display the error message coming from the try
block, I just added a message to identify the source, but I don’t know the exact error.
- By the way, the scenario is that I’m trying to extract data from D365 F&O. I’m using
insert record set
andupdate record set
to implement data into a table, since we have more than 6 million rows. Then, I transform the regular table into a temporary table to iterate over it. I face the same error when I loop over the regular table. When I get to theio.write
, it sometimes works, but most of the time, I run into this error. I don’t know exactly what causes the error because I haven’t found a way to display the error message from thetry
block. I just added a message to help me identify where the issue occurs, but I’m still unsure about the exact error
The problem I’m facing is that I want to use just one catch
block to handle all errors in the try
block and print the error message that is returned from the try
block (especially for the errors in io.writeExp
). But sometimes the errors do not return any message, and I can’t seem to capture them correctly.
Solution I’m Looking For:
- I need a way to catch all errors with a single
catch
block and print the message associated with the error , even when some errors don’t return a message. - I want to avoid multiple nested
try-catch
blocks and have just one catch for the entire operation.
Could someone help me find a way to achieve this with one catch
block that catches all errors and prints the appropriate error message?
Thanks in advance!