Recent

Author Topic: About the FPC feature  (Read 807 times)

nouzi

  • Full Member
  • ***
  • Posts: 142
About the FPC feature
« on: April 14, 2019, 10:15:04 am »
About the FPC feature
You could see this expression
Code: Pascal  [Select]
  1. try
  2.  
  3.    try
  4.  
  5.    finally
  6.  
  7.    end;
  8.  
  9. Except
  10. end;
  11.  
In one expression
Code: Pascal  [Select]
  1. try
  2.   finally
  3.   Except
  4. end;
  5.  
or
Code: Pascal  [Select]
  1. [code=pascal]
  2. try
  3.  Except
  4.  finally
  5. end;
  6.  
[/code]
Translate by Google
my english is  bad
Lazarus 2.0 free pascal 3.0.4
Lazarus trunk  free pascal trunk 
System : windows 7 64bit

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: About the FPC feature
« Reply #1 on: April 14, 2019, 10:24:02 am »
About the FPC feature
Try finally end; is used to make sure a reference counted or dynamically allocated resource is always released, e.g:
Code: Pascal  [Select]
  1. var a:Tobject;
  2. begin
  3.   a:=Tobject.create;
  4.   try
  5.      // do something with a.
  6.   finally
  7.     a.free; Now after use clean up the instance
  8.   end;
Try except end; is to catch any exceptions that can be caused in the try block and handle those gracefully, e.g.
Code: Pascal  [Select]
  1. var a,b:integer;
  2. begin
  3.    try
  4.       a:=1;
  5.       b:=0;
  6.       a :=a div b;
  7.    except
  8.        writeln('division by zero');
  9.    end;
Your last example is nonsense and should be:
Code: Pascal  [Select]
  1. try
  2.   try
  3.   except
  4.   end;
  5. finally
  6. end;
   
« Last Edit: April 14, 2019, 10:28:11 am by Thaddy »
also related to equus asinus.

nouzi

  • Full Member
  • ***
  • Posts: 142
Re: About the FPC feature
« Reply #2 on: April 14, 2019, 10:47:22 am »
What if I merging the first and second expressions? According to my understanding we get

Code: Pascal  [Select]
  1. var
  2.  a,b:integer;
  3.  c:Tobject;
  4.   begin
  5.     c:=Tobject.create;
  6.  
  7.     try
  8.        // do something with a.
  9.     finally
  10.       c.free; //Now after use clean up the instance
  11.     end;
  12.  
  13.       try
  14.  
  15.         a:=1;
  16.         b:=0;
  17.         a :=a div b;
  18.      except
  19.          writeln('division by zero');
  20.      end;  
  21.  

or this code
Code: Pascal  [Select]
  1.  try
  2.  
  3.  
  4.           try
  5.              // do something with a.
  6.           finally
  7.             c.free; //Now after use clean up the instance
  8.           end;
  9.  
  10.  
  11.  
  12.         a:=1;
  13.         b:=0;
  14.         a :=a div b;
  15.      except
  16.          writeln('division by zero');
  17.      end;              
  18.  

What the inhibitor would be like this

Code: Pascal  [Select]
  1.    c:=Tobject.create;
  2.      try
  3.  
  4.  
  5.        //   try     remove this line
  6.              // do something with a.
  7.           finally
  8.             c.free; //Now after use clean up the instance
  9.        //   end;  remove this line
  10.  
  11.  
  12.  
  13.         a:=1;
  14.         b:=0;
  15.         a :=a div b;
  16.      except
  17.          writeln('division by zero');
  18.      end;
  19.  
Translate from Google
my english is  bad
Lazarus 2.0 free pascal 3.0.4
Lazarus trunk  free pascal trunk 
System : windows 7 64bit

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: About the FPC feature
« Reply #3 on: April 14, 2019, 11:00:58 am »
No. try's are separate blocks and need their own try and end. Look at my examples. You can not short-cut by leaving either a try or an end out! My examples are correct.
also related to equus asinus.

nouzi

  • Full Member
  • ***
  • Posts: 142
Re: About the FPC feature
« Reply #4 on: April 14, 2019, 11:11:02 am »
@Thaddy thank
It was just a suggestion
my english is  bad
Lazarus 2.0 free pascal 3.0.4
Lazarus trunk  free pascal trunk 
System : windows 7 64bit

Thaddy

  • Hero Member
  • *****
  • Posts: 9293
Re: About the FPC feature
« Reply #5 on: April 14, 2019, 11:21:52 am »
The reason is they are syntactically different things.
also related to equus asinus.