Can Delphi tell me the name of the routine that chose the exception?

I know how to catch exceptions in delphi (try..except / finally and e.message), but I want to know if there is an exception handling mechanism that can throw an exception, as well as the name of the routine, using an example

procedure/function bla();//this can be in a unit/class
begin
 code....
 an error  is raised here -> inside or not of an try-except/finally block 
end;

and I will get a message / object / everything that indicates that the error 'x' was raised in 'bla'.

I know about makexcept, is there any other way to catch exceptions like these?

I am using Delphi 7. The solution may be applicable to another version of Delphi.

+3
source share
7 answers

ProcByLevel JclDebug.pas, JCL.

, 'Insert JDBG data into the binary' Ide- > jcl.

alt text

Uses
 JclDebug;

procedure TForm18.Button1Click(Sender: TObject);
begin
    try

       raise  Exception.Create('Hello from '+ProcByLevel(0));
    except
         on e : Exception do
         ShowMessage(e.Message);
    end;
end;

:

alt text

+7

EurekaLog , MadExcept.

+7

, MadExcept, , .

Delphi , , .

, :

, .

+6

JCLDebug JCLLastExceptStackListToStrings(), .

, TheNewbie, , TApplication.OnException .

+5

, Delphi " ". MadExcept , , .

+3

MadExcept - , , , , , Project JEDI JCL ( JclDebug.pas). . .MAP, TurboDebugger .. , ( MadExcept).

TApplication.OnException( ).

+3

... JCL ...

In your application ... You need to configure some information to get the necessary framework for the hook ...
Project → Compiler-> Stack Frames ... I also checked all the debugging and add the following ... Project-> Options-> Linker -> Map file (select Details) / Enable TD32 debugging information

In my Logger block ... I have this ... You must have your own TLogger ... which stores the information for you ...

use
  JclDebug, JclHookExcept;


procedure HookGlobalException(ExceptObj: TObject; ExceptAddr: Pointer; OSException: Boolean);
var
  a_List: TStringList;
  a_Error: string;
begin
  if Assigned(TLogger._Instance) then
  begin
    a_List := TStringList.Create;
    try
      a_List.Add(cStar);
      a_Error := Exception(ExceptObj).Message;
      a_List.Add(Format('{ Exception - %s }', [a_Error]));
      JclLastExceptStackListToStrings(a_List, False, True, True, False);
      a_List.Add(cStar);
      // save the error with stack log to file
      TLogger._Instance.AddError(a_List);
    finally
      a_List.Free;
      Raise Exception.Create(a_Error);
    end;
  end;
end;

initialization
  Lock := TCriticalSection.Create;
  Include(JclStackTrackingOptions, stTraceAllExceptions);
  Include(JclStackTrackingOptions, stRawMode);

  // Initialize Exception tracking
  JclStartExceptionTracking;

  JclAddExceptNotifier(HookGlobalException, npFirstChain);
  JclHookExceptions;

finalization
  JclUnhookExceptions;
  JclStopExceptionTracking;
  Lock.Free;

end.
+2
source

Source: https://habr.com/ru/post/1774134/


All Articles