Recent

Author Topic: Try Except/Finally Block  (Read 1173 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Try Except/Finally Block
« on: July 15, 2020, 06:40:00 am »
Try Except/Finally statements are the things I rarely use and haven't mastered.

Below is the code I used to fetch a web page, here is how it works:
- It uses TFPHTTPClieant to download a page
- The function's result is a string, so I can do some processing further
- If the server provides gzip data, save it to a temp file and uncompress it using TGZFileStream

The function it is working good. But I know it should use some try except and finally blocks. Where should I put them?

Extra question. I searched the web but failed to find code for direct uncompressing gzip stream. Do you know any better code for uncompressing gzip stream from web (without saving it to a temp file)?

Code: Pascal  [Select][+][-]
  1. function FetchPage: string;
  2. const
  3.   URL     = 'http://example.com/';
  4.   TmpFile = 'downloaded.tmp';
  5. var
  6.   Decompressor: TGZFileStream;
  7.   strStream:    TStringStream;
  8. begin
  9.   with TFPHTTPClient.Create(nil) do
  10.   begin
  11.     AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  12.     AddHeader('Accept-Encoding', 'gzip');
  13.     strStream := TStringStream.Create;
  14.     Get(URL, strStream);
  15.     if ResponseHeaders.Values['Content-Encoding'].Trim = 'gzip' then
  16.     begin
  17.       strStream.SaveToFile(TmpFile);
  18.       strStream.Clear;
  19.       Decompressor := TGZFileStream.create(TmpFile, gzopenread);
  20.       strStream.CopyFrom(Decompressor, 0);
  21.       Decompressor.Free;
  22.     end;
  23.     Result := strStream.DataString;
  24.     strStream.Free;
  25.     Free;
  26.   end;
  27. //  ShowMessage(Result);
  28. end;
« Last Edit: July 15, 2020, 06:42:26 am by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Try Except/Finally Block
« Reply #1 on: July 15, 2020, 08:12:51 am »
Perhaps like this:

Code: Pascal  [Select][+][-]
  1. function FetchPage: string;
  2. const
  3.   URL     = 'http://example.com/';
  4.   TmpFile = 'downloaded.tmp';
  5. var
  6.   Decompressor: TGZFileStream;
  7.   strStream:    TStringStream;
  8. begin
  9.   with TFPHTTPClient.Create(nil) do
  10.   try
  11.     AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  12.     AddHeader('Accept-Encoding', 'gzip');
  13.     strStream := TStringStream.Create;
  14.     try
  15.       Get(URL, strStream);
  16.       if ResponseHeaders.Values['Content-Encoding'].Trim = 'gzip' then
  17.       begin
  18.         strStream.SaveToFile(TmpFile);
  19.         strStream.Clear;
  20.         Decompressor := TGZFileStream.create(TmpFile, gzopenread);
  21.         strStream.CopyFrom(Decompressor, 0);
  22.         Decompressor.Free;
  23.       end;
  24.       Result := strStream.DataString;
  25.     finally
  26.       strStream.Free;
  27.   finally
  28.     Free;
  29.   end;
  30. //  ShowMessage(Result);
  31. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Try Except/Finally Block
« Reply #2 on: July 15, 2020, 09:43:37 am »
Hello Handoko,
when I started with exceptions, I found this pages very useful. Maybe they can help you too:
http://www.delphibasics.co.uk/RTL.asp?Name=try
http://www.delphibasics.co.uk/Article.asp?Name=Exceptions

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Try Except/Finally Block
« Reply #3 on: July 15, 2020, 06:09:31 pm »
Perhaps like this:
And also
Code: Pascal  [Select][+][-]
  1.         Decompressor := TGZFileStream.Create(TmpFile, gzopenread);
  2.         try
  3.           strStream.CopyFrom(Decompressor, 0);
  4.         finally
  5.           Decompressor.Free;
  6.         end;

 

TinyPortal © 2005-2018