From Clomosy Docs

(Created page with "It is a structure used to make programs more secure and resilient against errors. While try...finally blocks are crucial for resource management, try...except blocks are critical for error handling. This structure groups code blocks, enabling the program to handle unexpected situations. *'''try''': The code written inside this block is for code that carries the potential for errors or may throw exceptions. If an error occurs or an exception is raised while the program i...")
 
No edit summary
Line 71: Line 71:
   '''end;'''
   '''end;'''
  end;
  end;
:'''TRObject Syntax'''
:'''TRObject Syntax'''
  var
  var
Line 87: Line 88:
       number = 0;
       number = 0;
     }
     }
  '''}'''
}
== Try...finally Blocks ==
When an error occurs in a code block, it is used to catch and handle that error. The except block following the try block is executed when an exception is thrown. This allows the program to handle the error in a more controlled manner.
'''Example:'''<br>
Let's create an example using the except block to achieve the same result as the example with the try...finally block.
:'''Basic Syntax'''
var
  number, zero : Integer;
begin
  // Try to divide an integer by zero - to raise an exception
  '''Try'''
    zero  := 0;
    number := 1 div zero;
    ShowMessage('number / zero = '+IntToStr(number));
  '''except'''
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  '''end;'''
end;
:'''TRObject Syntax'''
var
  number, zero : Integer;
{
  // Try to divide an integer by zero - to raise an exception
  '''Try'''
    zero  = 0;
    number = 1 div zero;
    ShowMessage('number / zero = '+IntToStr(number));
  '''except{'''
    ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
   '''}'''
   '''}'''
  }
  }

Revision as of 12:10, 14 November 2023

It is a structure used to make programs more secure and resilient against errors. While try...finally blocks are crucial for resource management, try...except blocks are critical for error handling. This structure groups code blocks, enabling the program to handle unexpected situations.

  • try: The code written inside this block is for code that carries the potential for errors or may throw exceptions. If an error occurs or an exception is raised while the program is running within this block, control passes to the except block.
  • except: This block is used to handle and process errors that occur within the try block. Specific error types can be identified, or a general Exception block can be used to define specific actions for different error situations.
  • finally: This block contains code that will be executed regardless of what happens in the try block (whether it runs without errors or encounters an error). Typically, cleanup operations, releasing resources, and similar tasks are defined here.

Try...finally Blocks

try...finally blocks are used to execute a code block whether it completes successfully or not. These blocks are particularly useful for cleanup operations, such as releasing resources.

The code inside the try block will run regardless of whether an error is thrown or not, and the code inside the finally block will execute in all situations.

Example 1:
An example where a TclStringList is created within the try block, and then, regardless of whether an exception is thrown or not, the finally block releases the TclStringList. This is especially used to safely free dynamically created objects.

Basic Syntax
var
 MyList: TclStringList;
begin
 MyList := Clomosy.StringListNew;

 try
  MyList.Add('Blue');
  MyList.Add('Red');
  MyList.Add('Yellow');
  MyList.Add('Purple'); 

 finally
  ShowMessage('Items: ' + MyList.Text);
  MyList.Free;
 end;
end;
TRObject Syntax
var
 MyList: TclStringList;
{
 MyList = Clomosy.StringListNew;

 try
  MyList.Add('Blue');
  MyList.Add('Red');
  MyList.Add('Yellow');
  MyList.Add('Purple');
 finally
 {
  ShowMessage('Items: ' + MyList.Text);
  MyList.Free;
 }
}

Example 2:
Let's create a code example where a division by zero error is intentionally triggered using the finally clause. Since error handling is not enabled, when finally is used, the program returns the error 'Division by zero when evaluating instruction PushVar ($0,$0,$0,$0,'Result').'.

Basic Syntax
var
 number, zero : Integer;
begin
 // Try to divide an integer by zero - to raise an exception
 number := -1;
 Try
   zero   := 0;
   number := 1 div zero;
   ShowMessage('number / zero = '+IntToStr(number));
 finally
   if number = -1 then
   begin
     ShowMessage('Number was not assigned a value - using default');
     number := 0;
   end;
 end;
end;
TRObject Syntax
var
 number, zero : Integer;
{
 // Try to divide an integer by zero - to raise an exception
 number = -1;
 Try
   zero   = 0;
   number = 1 div zero;
   ShowMessage('number / zero = '+IntToStr(number));
 finally{
   if (number == -1)
   {
     ShowMessage('Number was not assigned a value - using default');
     number = 0;
   }
 }
}

Try...finally Blocks

When an error occurs in a code block, it is used to catch and handle that error. The except block following the try block is executed when an exception is thrown. This allows the program to handle the error in a more controlled manner.

Example:
Let's create an example using the except block to achieve the same result as the example with the try...finally block.

Basic Syntax
var
 number, zero : Integer;
begin
 // Try to divide an integer by zero - to raise an exception
 Try
   zero   := 0;
   number := 1 div zero;
   ShowMessage('number / zero = '+IntToStr(number));
 except
   ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
 end;
end;
TRObject Syntax
var
 number, zero : Integer;
{
 // Try to divide an integer by zero - to raise an exception
 Try
   zero   = 0;
   number = 1 div zero;
   ShowMessage('number / zero = '+IntToStr(number));
 except{
   ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
 }
}