how to handle an exception dropped by a web-server? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Nicole · 2025-06-05 19:04 There is a working function for picking stock information by ticker- request: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  HttpClient: TFPHTTPClient;  URL: string;  Response: string;begin  result:=nil;  HttpClient := TFPHTTPClient.Create(nil);  // The program uses TFPHTTPClient to send a GET request to the Finnhub API.  URL:='https://finnhub.io/api/v1/stock/profile2?symbol=' + Ticker + '&token=' + API_KEY;  Response := HttpClient.Get(URL);      Result:=GetJSON(Response);  HttpClient.Free;end; is works fine, as long, as there is a valid ticker. If there is no valid ticker, the response ist 404, 309, 401 or so on. Sure, the reason is the wrong ticker. However no user wants to read "an exception occured". How can I catch the error 404 (or similar) and make it a nice ShowMessage('ticker not valid: ' + ticker); instead. ---------------------------------------------------------------------- Leledumbo · 2025-06-05 19:29 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+} uses  sysutils,  opensslsockets,  fphttpclient; const  URL = 'https://finnhub.io/api/v1/stock/profile2?symbol=xxx&token=yyy';  begin  with TFPHTTPClient.Create(nil) do    try      try        WriteLn(Get(URL));      except        on e: EHTTPClient do begin        case e.StatusCode of            401: WriteLn('Unauthorized, supply the correct token');            404: WriteLn('What the heck are you hitting, bro? I have no such a thing');            309: WriteLn('Honestly, this is non-standard so the interpretation is up to the server code writer');            otherwise WriteLn(e.StatusCode, ': ' + e.StatusText);          end;        end;      end;    finally    Free;    end;end. ---------------------------------------------------------------------- Nicole · 2025-06-06 10:47 thank you. What is puzzling me: 2 try after each other? ---------------------------------------------------------------------- egsuh · 2025-06-06 11:46 Inner one is try...except, outer one is try... finally block. ---------------------------------------------------------------------- Leledumbo · 2025-06-06 11:53 Quote from: Nicole on June 06, 2025, 12:47:15 pmthank you. What is puzzling me: 2 try after each other? AFAIK we still don't have, and AFAIR it's been decided that we'll never have unless Delphi does it first, a unified try-except-finally. So try-finally and try-except are two distinct constructs in which you can decide the execution order by changing the nesting order. Well, actually you can nest any as many as you wish sans the practical limit (32?), it just so happens here in my particular example that try- finally is used to ensure Free to be called for TFPHTTPClient instance while try-except is used to handle the exception based on non-200 status code, which is the default (overridable by AllowRedirect for 301/307, the rest must use custom call to HTTPMethod).