Forum > General
[Solved] Throw error if string is empty
nikel:
Hello I know that 0 * 0 = Meaningless. But below code doesn't work:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---try Num:=Length(TempStrPtr^) * 0;except on E: Exception do begin raise Exception.Create('KEY or TKEY tag not found. ' + #13#10 + E.Message); CreateIndexUnset; Halt(-1); end;end;
How can I make it throw an error when string is null?
Thaddy:
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).
KodeZwerg:
Idk... idk...
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project1; {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF} uses SysUtils; type TStringObject = class(TObject) strict private FAnsiString: AnsiString; FWideString: WideString; private procedure SetAnsiString(const AValue: AnsiString); procedure SetWideString(const AValue: WideString); public constructor Create; public property Ansi: AnsiString read FAnsiString write SetAnsiString; property Wide: WideString read FWideString write SetWideString; end; constructor TStringObject.Create;begin inherited Create; FAnsiString := ''; FWideString := '';end; procedure TStringObject.SetAnsiString(const AValue: AnsiString);begin if (AValue <> '') then FAnsiString := AValue else raise Exception.Create('Can not be Empty!');end; procedure TStringObject.SetWideString(const AValue: WideString);begin if (AValue <> '') then FWideString := AValue else raise Exception.Create('Can not be Empty!');end; var so: TStringObject;begin so := TStringObject.Create; try WriteLn('Try'); try so.Ansi := 'Next raise an exception.'; WriteLn(so.Ansi); so.Ansi := ''; // *boom* WriteLn(so.Ansi); except WriteLn('Exception'); end; finally so.Free; WriteLn('Finally'); end; WriteLn('Press Return'); ReadLn;end.
Bart:
Why do you insist on using pointers to strings?
Bart
jamie:
--- Quote from: Bart on December 09, 2022, 03:38:45 pm ---Why do you insist on using pointers to strings?
Bart
--- End quote ---
He's probably an old cuke/fart like most of us here.
Navigation
[0] Message Index
[#] Next page