Handling errors with TryFunctions
In traditional programming languages like C#, you can handle errors in a code block by using a Try – Catch clause:
try
{
//Your code here
}
catch(Exception)
{
//Error handling here
}
In AL language you can use Try methods for something similar. A Try method permits you to handle errors that occurs during a code execution. Database changes (writes) that are executed during a Try method are not rolled back (if a try method contains a database write transaction, a runtime error occurs).Let’s consider the following piece of AL code written inside a codeunit object:
[TryFunction]
local procedure MyTryMethod()
begin
//Code here
if (condition) then
error('An error occurred during the operation');
end;
trigger OnRun()
begin
if MyTryMethod then
message('Process completed successfully.')
else
message('Something went wrong on the process.')
end;
Here we have defined a procedure...