Page 1 of 1

Manipulate madexcept.Exceptmessage

Posted: Thu May 12, 2022 7:52 am
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

Re: Manipulate madexcept.Exceptmessage

Posted: Thu May 12, 2022 8:15 am
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?

Re: Manipulate madexcept.Exceptmessage

Posted: Thu May 12, 2022 9:40 am
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.

Re: Manipulate madexcept.Exceptmessage

Posted: Thu May 12, 2022 11:40 am
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?

Re: Manipulate madexcept.Exceptmessage

Posted: Mon May 16, 2022 7:50 am
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.

Re: Manipulate madexcept.Exceptmessage

Posted: Mon May 16, 2022 8:57 am
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...

Re: Manipulate madexcept.Exceptmessage

Posted: Wed May 18, 2022 1:05 pm
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.

Re: Manipulate madexcept.Exceptmessage

Posted: Wed May 18, 2022 2:07 pm
by madshi
Haha, all good... :D