Manipulate madexcept.Exceptmessage

delphi package - automated exception handling
Post Reply
ctvt
Posts: 4
Joined: Thu May 12, 2022 7:42 am

Manipulate madexcept.Exceptmessage

Post by ctvt »

Hello,

i have a short question about the property 'exceptInf.ExceptMessage'.

Whe're creating our own bugreport with contents of ExceptMessage, ExceptClass and RelatedObject.
I realised, that when an exception occurs, the first line of ExceptMessage already contains the relatedobject - if its not a TComponent.

Is it possible to manipulate the contents of ExceptMessage in that way, that theres no additional information about the name of the object?
Else i have to delete the first line by hand, which led to asking in this forum. Maybe we're doing something wrong :)

Thanks in advance
madshi
Site Admin
Posts: 10526
Joined: Sun Mar 21, 2004 5:25 pm

Re: Manipulate madexcept.Exceptmessage

Post by madshi »

It's a bit hard to comment on this without having any real hard data. What is the ExceptMessage exactly? What is the relatedobject? And why do you need to change ExceptMessage? If you create your own bugreport, anyway, then it shouldn't matter what ExceptMessage is set to?
ctvt
Posts: 4
Joined: Thu May 12, 2022 7:42 am

Re: Manipulate madexcept.Exceptmessage

Post by ctvt »

Added a small example. That's sort of how we add some information to already given exception-information and show it in a message-box.

Code: Select all

initialization
  exHandler := TEHandler.Create;
  RegisterExceptionHandler(OnBeforeDialog, stTrySyncCallAlways, epMainPhase);

finalization
  FreeAndNil(exHandler);

procedure OnBeforeDialog(const exceptIntf: IMEException; var handled: boolean);
var
  errMsg: string;
begin
  errMsg := GetErrorMessage(exceptIntf.ExceptMessage, exceptIntf.ExceptClass, exceptIntf.RelatedObject);
  ErrorDialog.Excecute(errMsg);
end;


function GetErrorMessage(exceptMsg: string; exceptClassName: string; exceptObj: TObject): string;
var
  additionalInfos: string;
  objinfos: string;
begin
  additionalInfos := 'OS version:' + Linebreak + OS.Info.GetText + Linebreak + Linebreak;
  additionalInfos := additionalInfos + 'Software version:' + Linebreak + OurSoftware.Name + Linebreak + Linebreak;

  if not Assigned(exceptObj) then
    objinfos := Format('%s'), ['nil']);
  else
  if exceptObj is TOurCustomObjekt then
    objinfos := Format('%s [%s]', [TOurCustomObjekt(exceptObj).Name, exceptObj.ClassName])
  else
  if exceptObj is TComponent then
    objinfos := Format('%s [%s]', [TComponent(exceptObj).Name, exceptObj.ClassName])
  else
    objinfos := Format('%s'), ['nil']);
  additionalInfos := additionalInfos + 'Exception in:'+ Linebreak + objinfos + Linebreak + Linebreak;

  // Fix because output is different when exceptObj is TOurCustomObjekt
  if exceptObj is TOurCustomObjekt then
  begin
    firstline := System.Pos(LineBreak, ExceptMessage);
    System.Delete(ExceptMessage, 1, firstline + 1);
  end;

  Result := 'Some error occured: '+ Linebreak + exceptMsg + Linebreak + additionalInfos;
end;
If it's a TOurCustomObjekt, then ExceptMessage will be:
'TestObject ano domini'#$D#$A'Zugriffsverletzung bei Adresse 0000000001ED8DEC in Modul ''OurSoftware.exe''. Lesen von Adresse 00000000000004EC.'
Anything else, e.g. a TListView (TComponent):
'Zugriffsverletzung bei Adresse 0000000001EC6EE1 in Modul ''OurSoftware.exe''. Schreiben von Adresse 0000000000000730.'

Sure i can cut off this first line, but maybe i misunderstood something using ExceptMessage.
madshi
Site Admin
Posts: 10526
Joined: Sun Mar 21, 2004 5:25 pm

Re: Manipulate madexcept.Exceptmessage

Post by madshi »

I don't think I've seen that before. What happens if you completely disable madExcept. Does the exception message then still include that first line starting with "TestObject ano domini"? I wonder if it's Delphi which sometimes adds this first line for some reason?
ctvt
Posts: 4
Joined: Thu May 12, 2022 7:42 am

Re: Manipulate madexcept.Exceptmessage

Post by ctvt »

I tested it again with Delphi 10.4.2 without Madexcept.

Our previous exception-handler, where we directly caught the Exception-Message - didn't show off this extra line with 'objectname'#DA.

It's not that much, but if you say so, i'll catch this and delete this extra line. No problem.
madshi
Site Admin
Posts: 10526
Joined: Sun Mar 21, 2004 5:25 pm

Re: Manipulate madexcept.Exceptmessage

Post by madshi »

Looking at madExcept's source code, I don't seem to find any place where I would add this line. One thing you could try is to copy "madExcept.pas" and "mad.inc" into your project folder (which allows you to set breakpoints into the madExcept code), and then set a breakpoint to "madExcept.GetExceptMessage_()". That way you can check how madExcept calculates the exception message. Is that extra line already there? If so, maybe one of your try..except blocks adds that line? No idea...
ctvt
Posts: 4
Joined: Thu May 12, 2022 7:42 am

Re: Manipulate madexcept.Exceptmessage

Post by ctvt »

I'm very sorry, one of my colleagues pre-handled the message and put the objectname into your exceptionmessage.
Thanks for your help :) Closed here.
madshi
Site Admin
Posts: 10526
Joined: Sun Mar 21, 2004 5:25 pm

Re: Manipulate madexcept.Exceptmessage

Post by madshi »

Haha, all good... :D
Post Reply