Try
Deutsch · English · Español · Suomi · Русский
Back to Reserved words.
The reserved word try is part of either a try..finally block or a try..except block.
If an exception occurs while executing the code between try and finally, execution resumes at finally. If no exception occurs, the code between finally and end will be executed also.
try
// code that might generate an exception
finally
// will always be executed as last statements
end;Whenever an exception occurs, the code between except and end will be executed.
try
// code that might generate an exception
except
// will only be executed in case of an exception
on E: EDatabaseError do
ShowMessage( 'Database error: '+ E.ClassName + #13#10 + E.Message );
on E: Exception do
ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
end;To handle exceptions with an except block and also run a finally block, nest one try block in another.
try
try
// code dealing with database that might generate an exception
except
// will only be executed in case of an exception
on E: EDatabaseError do
ShowMessage( 'Database error: '+ E.ClassName + #13#10 + E.Message );
on E: Exception do
ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
end;
finally
// clean up database-related resources
end;See also
From the Free Pascal wiki, page "Try", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
