Recent

Author Topic: [Solved] Throw error if string is empty  (Read 860 times)

nikel

  • Full Member
  • ***
  • Posts: 186
[Solved] Throw error if string is empty
« on: December 09, 2022, 01:34:48 am »
Hello I know that 0 * 0 = Meaningless. But below code doesn't work:

Code: Pascal  [Select][+][-]
  1. try
  2.   Num:=Length(TempStrPtr^) * 0;
  3. except
  4.   on E: Exception do
  5.   begin
  6.     raise Exception.Create('KEY or TKEY tag not found. ' + #13#10 + E.Message);
  7.     CreateIndexUnset;
  8.     Halt(-1);
  9.   end;
  10. end;

How can I make it throw an error when string is null?
« Last Edit: December 09, 2022, 09:07:20 pm by nikel »

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Throw error if string is empty
« Reply #1 on: December 09, 2022, 01:59:21 am »
Congratulations! Almost every line of code is dubious or an error.
The most important error being line 6, so line 7 and 8 are never executed.
But line 2 is also a point of concern, without proper context it may or may not "work", but it is horrible coding (and I do not mean the silly multiplication).
« Last Edit: December 09, 2022, 02:11:52 am by Thaddy »
Specialize a type, not a var.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Throw error if string is empty
« Reply #2 on: December 09, 2022, 03:20:17 am »
Idk... idk...
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. type
  9.   TStringObject = class(TObject)
  10.     strict private
  11.       FAnsiString: AnsiString;
  12.       FWideString: WideString;
  13.     private
  14.       procedure SetAnsiString(const AValue: AnsiString);
  15.       procedure SetWideString(const AValue: WideString);
  16.     public
  17.       constructor Create;
  18.     public
  19.       property Ansi: AnsiString read FAnsiString write SetAnsiString;
  20.       property Wide: WideString read FWideString write SetWideString;
  21.   end;
  22.  
  23. constructor TStringObject.Create;
  24. begin
  25.   inherited Create;
  26.   FAnsiString := '';
  27.   FWideString := '';
  28. end;
  29.  
  30. procedure TStringObject.SetAnsiString(const AValue: AnsiString);
  31. begin
  32.   if (AValue <> '') then
  33.     FAnsiString := AValue
  34.     else
  35.     raise Exception.Create('Can not be Empty!');
  36. end;
  37.  
  38. procedure TStringObject.SetWideString(const AValue: WideString);
  39. begin
  40.   if (AValue <> '') then
  41.     FWideString := AValue
  42.     else
  43.     raise Exception.Create('Can not be Empty!');
  44. end;
  45.  
  46. var
  47.   so: TStringObject;
  48. begin
  49.   so := TStringObject.Create;
  50.   try
  51.     WriteLn('Try');
  52.     try
  53.       so.Ansi := 'Next raise an exception.';
  54.       WriteLn(so.Ansi);
  55.       so.Ansi := ''; // *boom*
  56.       WriteLn(so.Ansi);
  57.     except
  58.       WriteLn('Exception');
  59.     end;
  60.   finally
  61.     so.Free;
  62.     WriteLn('Finally');
  63.   end;
  64.   WriteLn('Press Return');
  65.   ReadLn;
  66. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Throw error if string is empty
« Reply #3 on: December 09, 2022, 03:38:45 pm »
Why do you insist on using pointers to strings?

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Throw error if string is empty
« Reply #4 on: December 09, 2022, 03:46:30 pm »
Why do you insist on using pointers to strings?

Bart

He's probably an old cuke/fart like most of us here.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: Throw error if string is empty
« Reply #5 on: December 09, 2022, 06:23:32 pm »
No he comes from null instead of nil. Read the original post. Anyway, good enough code for a T-shirt with a red cross allover it.  8-)
Specialize a type, not a var.

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Throw error if string is empty
« Reply #6 on: December 09, 2022, 09:06:03 pm »
I tried it one more time and it worked:

Code: Pascal  [Select][+][-]
  1. try
  2.   WriteLn(Length(BpmPtr^) / Length(BpmPtr^));
  3. except
  4.   on E: Exception do
  5.   begin
  6.     raise Exception.Create('bpm tag not found. ' + #13#10 + E.Message);
  7.     GetAllTagsByPathUnset;
  8.     Halt(-1);
  9.   end;
  10. end;

Thanks for the replies.

 

TinyPortal © 2005-2018