Forum > General
Any way to intercept stdout/stderr?
TRon:
--- Quote from: rishav on June 04, 2023, 10:40:31 pm ---I was hoping that it would be easier to intercept error messages than putting everything in a large try catch. :(
--- End quote ---
The developer is responsible for handling errors so in those cases it does output something it is your own fault :P
But since you changed the requirement to handling unhandled exceptions:
--- 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 catchyrhymes; {$mode objfpc}{$H+} uses sysutils, classes; var rhymes: TStringList = nil; procedure CatchNonHandledExceptions(Obj : TObject; Addr: CodePointer; FrameCount: Longint; Frames: PCodePointer);begin rhymes.Add('You have stumbled upon an unhandled exception');end; procedure FamousLastWords;begin if assigned(rhymes) then begin if rhymes.Count > 0 then begin writeln('---'); writeln(rhymes.Text); writeln('---') end; FreeAndNil(rhymes) end;end; procedure SmartCalculation;var x,y:integer;begin y:= 0; x:= 1 div yend; begin rhymes:= TStringList.Create; System.AddExitProc(@FamousLastWords); System.ExceptProc:= @CatchNonHandledExceptions; writeln('To calculate'); SmartCalculation; writeln('or not to calculate')end.
BobDog:
I don't use Lazarus ide, but Geany ide will keep the console open after a crash.
In windows the command to run the .exe is
'cmd /c '+ file.exe +'& pause'
Example:
--- 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";}};} --- uses sysutils; function system(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system'; function filelength(filename:ansistring):longword;Var F : File Of byte;var L:longword;beginAssign (F,filename); Reset (F); L:=FileSize(F); Close (F); exit(L);end; function loadfile(filename:ansistring):ansistring; Var Fin : File; Var x:longint; var content:ansistring=''; begin x:=filelength(filename); setlength(content,x); Assign (Fin,filename); Reset (Fin,x); BlockRead (Fin,content[1],1); close(fin); exit(content);end; procedure savefile(s:ansistring ;filename:ansistring); var fout:file; begin Assign(fout,filename); Rewrite(fout,length(s)); blockwrite(fout,s[1],1); close(fout); end; varcode:ansistring;pas:ansistring;s:ansistring; beginpas:=getcurrentdir+'\heloo.pas'; code:='var x,y:integer;'+chr(10);code:=code +'begin'+chr(10);code:=code+'y:=0;'+chr(10);code:=code+' x:= 1 div y;'+chr(10);code:=code+'readln;'+chr(10);code:=code+'end.'; savefile(code,pas);writeln(loadfile(pas));writeln(fileexists(pas));writeln;writeln; system(pchar('fpc -vwnhi '+ pas)); system(pchar('cmd /c '+getcurrentdir+'\heloo.exe' +'& pause')); end.
ccrause:
--- Quote from: TRon on June 05, 2023, 08:35:10 am ---
--- Quote from: rishav on June 04, 2023, 10:40:31 pm ---I was hoping that it would be easier to intercept error messages than putting everything in a large try catch. :(
--- End quote ---
The developer is responsible for handling errors so in those cases it does output something it is your own fault :P
But since you changed the requirement to handling unhandled exceptions:
--- 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";}};} --- System.ExceptProc:= @CatchNonHandledExceptions;
--- End quote ---
As TRon mentioned, you can hook into ExceptProc to catch unhandled exceptions. You can also hook into ErrorProc to catch runtime errors. This way you can customize the generated output.
rishav:
Thank you @ccrause and @TRon. I think ExceptProc and ErrorProc are exactly what I was looking for.
Thaddy:
AssignStream?
https://www.freepascal.org/docs-html/rtl/unix/assignstream.html
The text types are simply the handles for stdin/out/err
Linux only.
Navigation
[0] Message Index
[*] Previous page