Difference between revisions of "Try"

From Lazarus wiki
Jump to navigationJump to search
(Add try-try-except-finally nesting case.)
 
Line 5: Line 5:
  
  
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">try</syntaxhighlight> is part of either a <syntaxhighlight lang="pascal" enclose="none">try</syntaxhighlight>..[[Finally|<syntaxhighlight lang="pascal" enclose="none">finally</syntaxhighlight>]] [[Block|block]] or a  
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>try</syntaxhighlight> is part of either a <syntaxhighlight lang="pascal" inline>try</syntaxhighlight>..[[Finally|<syntaxhighlight lang="pascal" inline>finally</syntaxhighlight>]] [[Block|block]] or a  
<syntaxhighlight lang="pascal" enclose="none">try</syntaxhighlight>..[[Except|<syntaxhighlight lang="pascal" enclose="none">except</syntaxhighlight>]] block.
+
<syntaxhighlight lang="pascal" inline>try</syntaxhighlight>..[[Except|<syntaxhighlight lang="pascal" inline>except</syntaxhighlight>]] block.
  
If an [[Exceptions|exception]] occurs while executing the code between <syntaxhighlight lang="pascal" enclose="none">try</syntaxhighlight> and  
+
If an [[Exceptions|exception]] occurs while executing the code between <syntaxhighlight lang="pascal" inline>try</syntaxhighlight> and  
<syntaxhighlight lang="pascal" enclose="none">finally</syntaxhighlight>, execution resumes at <syntaxhighlight lang="pascal" enclose="none">finally</syntaxhighlight>. If no exception occurs, the code between <syntaxhighlight lang="pascal" enclose="none">finally</syntaxhighlight> and  
+
<syntaxhighlight lang="pascal" inline>finally</syntaxhighlight>, execution resumes at <syntaxhighlight lang="pascal" inline>finally</syntaxhighlight>. If no exception occurs, the code between <syntaxhighlight lang="pascal" inline>finally</syntaxhighlight> and  
[[End|<syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight>]] will be executed also.
+
[[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] will be executed also.
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 20: Line 20:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Whenever an exception occurs, the code between <syntaxhighlight lang="pascal" enclose="none">except</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">end</syntaxhighlight> will be executed.
+
Whenever an exception occurs, the code between <syntaxhighlight lang="pascal" inline>except</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>end</syntaxhighlight> will be executed.
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>
Line 34: Line 34:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
To handle exceptions with an <syntaxhighlight lang="pascal" enclose="none">except</syntaxhighlight> block '''and''' also run a <syntaxhighlight lang="pascal" enclose="none">finally</syntaxhighlight> block, nest one <syntaxhighlight lang="pascal" enclose="none">try</syntaxhighlight> block in another.
+
To handle exceptions with an <syntaxhighlight lang="pascal" inline>except</syntaxhighlight> block '''and''' also run a <syntaxhighlight lang="pascal" inline>finally</syntaxhighlight> block, nest one <syntaxhighlight lang="pascal" inline>try</syntaxhighlight> block in another.
  
 
<syntaxhighlight lang=pascal>
 
<syntaxhighlight lang=pascal>

Latest revision as of 17:24, 6 August 2022

Deutsch (de) English (en) español (es) suomi (fi) русский (ru)


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