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
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
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.
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.
*<b>try</b>: 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.
*<b>except</b>: 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.
*<b>finally</b>: 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 ==
<h2> Try...finally Blocks </h2>
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.
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.
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:'''<br>
<b>Example 1 </b><br>
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'''
<pre>
  var
Var
  MyList: TclStringList;
  Sayi, Sayi2,Toplam : Integer;
{
{
  MyList = Clomosy.StringListNew;
Sayi = 20;
   
  Sayi2 = 18;
  '''try'''
try
  MyList.Add('Blue');
    Toplam = Sayi + Sayi2;
  MyList.Add('Red');
finally
  MyList.Add('Yellow');
    ShowMessage('Toplama işlemi gerçekleştirildi.');
  MyList.Add('Purple');
    ShowMessage(IntToStr(Sayi)+' + '+IntToStr(Sayi2)+' = '+IntToStr(Toplam));
  '''finally'''
  '''{'''
  ShowMessage('Items: ' + MyList.Text);
  MyList.Free;
  '''}'''
  }
  }
}
</pre>


'''Example 2:'''<br>
 
<b>Example 2 </b><br>
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').'.
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'''
<pre>
var
var
  number, zero : Integer;
number, zero : Integer;
  begin
{
  // Try to divide an integer by zero - to raise an exception
  // Try to divide an integer by zero - to raise an exception
  number := -1;
number = -1;
  '''Try'''
Try
    zero  := 0;
  zero  = 0;
    number := 1 div zero;
  number = 1 div zero;
    ShowMessage('number / zero = '+IntToStr(number));
  ShowMessage('number / zero = '+IntToStr(number));
  '''finally'''
finally
    if number = -1 then
  if (number == -1)
    begin
  {
      ShowMessage('Number was not assigned a value - using default');
    ShowMessage('Number was not assigned a value - using default');
      number := 0;
    number = 0;
    end;
  }
  '''end;'''
}
end;
}
:'''TRObject Syntax'''
</pre>
var
 
  number, zero : Integer;
<h2> Try...except Blocks </h2>
{
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.
  // Try to divide an integer by zero - to raise an exception
 
  number = -1;
<b>Example</b><br>
  '''Try'''
Let's create an example using the except block to achieve the same result as the example with the try...finally block.
    zero  = 0;
 
    number = 1 div zero;
<pre>
    ShowMessage('number / zero = '+IntToStr(number));
var
  '''finally{'''
number, zero : Integer;
    if (number == -1)
{
    {
// Try to divide an integer by zero - to raise an exception
      ShowMessage('Number was not assigned a value - using default');
Try
      number = 0;
  zero  = 0;
    }
  number = 1 div zero;
  '''}'''
  ShowMessage('number / zero = '+IntToStr(number));
except
  ShowMessage('Exception Class: '+LastExceptionClassName+' Exception Message: '+LastExceptionMessage);
  }
  }
}
</pre>
{{#seo:|title=Error Trapping in Clomosy - Clomosy Docs}}
{{#seo:|description=Explore Error Trapping in Clomosy Docs! A comprehensive guide to managing and preventing errors for more robust mobile app development.}}

Latest revision as of 14:07, 23 December 2024

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

Var
 Sayi, Sayi2,Toplam : Integer;
{
 Sayi = 20;
 Sayi2 = 18;
 try
    Toplam = Sayi + Sayi2;
 finally
    ShowMessage('Toplama işlemi gerçekleştirildi.');
    ShowMessage(IntToStr(Sayi)+' + '+IntToStr(Sayi2)+' = '+IntToStr(Toplam));
 }
}


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').'.

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...except 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.

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);
 }
}