EWindowsException - Delphi

DarkCoderSc personal avatar
DarkCoderSc

Jean-Pierre LESUEUR

uses
  System.SysUtils, Winapi.Windows;

type
  EWindowsException = class(Exception)
  private
    FErrorCode : Integer;
  public
    {@C}
    constructor Create(const WindowsAPIName : String; const AErrorCode : Cardinal = 0); overload;

    {@G}
    property ErrorCode : Integer read FErrorCode;
end;

(* EWindowsException *)

constructor EWindowsException.Create(const WindowsAPIName : String; const AErrorCode : Cardinal = 0);
begin
  if AErrorCode = 0 then
    FErrorCode := GetLastError()
  else
    FErrorCode := AErrorCode;
  ///

  var AFormatedMessage := Format('___%s: last_err=%d, last_err_msg="%s".', [
      WindowsAPIName,
      FErrorCode,
      SysErrorMessage(FErrorCode)
  ]);

  ///
  inherited Create(AFormatedMessage);
end;

Creating and researching code snippets takes time and effort. You’re welcome to share them through your own platforms, but please don’t forget to credit the original author, here: Jean-Pierre LESUEUR.


Created

May 14, 2025

Last Revised

May 14, 2025