Recent

Author Topic: Using ApplicationProperties.OnException by TApplication.HandleException  (Read 487 times)

rockstein

  • Newbie
  • Posts: 5
Hello community,
I am sorry for a beginner question. I have the main form with a few other forms. I want to avoid using try except in each method, because of same code in except.  I want to build a common exception handler on each Form.  I see a loop in TApplication.HandleException through OnException events, but I miss any possibility to mark the exception as handled.

I placed the ApplicationProperties component on the FormX, FormY and FormZ and set the OnException event to the appropriate method in the From object.  In this method  I handle only exception, known in this form unit.

In TApplication.HandleException there is the loop through all methods in
Code: Pascal  [Select][+][-]
  1. FApplicationHandlers[ahtException]
which executes my events one by one.

Code: Pascal  [Select][+][-]
  1.        
  2. while FApplicationHandlers[ahtException].NextDownIndex(i) do
  3.           TExceptionEvent(FApplicationHandlers[ahtException][i])(Sender, Exception(ExceptObject));
  4.  


In case of an exception, the OnException events of FormX, FormY, and FormZ are executed one after other. In Event I do action only for known execption objects
Code: Pascal  [Select][+][-]
  1. if E is ESocketError then begin  
  2.   AddLog ((E as  ESocketError).Msg);
  3.   AddLog ((E as  ESocketError).Code);
  4. end;
  5. if E is EMyCoolExeption then begin  
  6.   AddLog ((E as  EMyCoolExeption).Msg);
  7.   AddLog ((E as  EMyCoolExeption).SomeChildMethod);
  8. end;
  9.  

But if the exception is not known, the ShowException(Exception(ExceptObject)) method is not executed in TApplication.HandleException. 

My beginner solution could be like this:
when I handle the exception I want to set some flag. At the last method of FApplicationHandlers[ahtException] Methodlist I want to look if the exception object was handled, if not then ShowException will be executed there.

May I use the "AppNoExceptionMessages" flag in ApplicationFFlags or it is used internal for other purpose?  May be exists some common approach for my case.  Thanks

Чебурашка

  • Hero Member
  • *****
  • Posts: 527
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
In general I'd say exceptions handled in a different place from where they are generated could make impossible to operate countermeasures other than notifications, as you loose the context. Also I'd say that if every method of the form generates an exception maybe exceptions are a little abused (as they seem to happen in all contexts), or you are missing a sublayer where your real operations are done, so that you can trap and manage exceptions at that level, so that you gui code doesn't have to trap and manage them all around.

But this is just a blind impression, maybe you could show some code to make the problem more understandable.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

rockstein

  • Newbie
  • Posts: 5
Спасибо за быстрый ответ, чебурашка ;)
you are missing a sublayer where your real operations are done
Yes, I think this is my case. I deal with jsonrpc and with json objects. On my Form I have many buttons with try except because many fcl units raises own exeptions, i.e.  fpjson, fphttpclient, fprpcclient. If I leave this exception unhadled -> showexeption in TApplication popups, but I need some actions depends exception. Currently, I wrote one common method and use it in all except blocks on the form to reduce redundant code. But  so-called "Try-Except" syndrome remains.
Exists any better solution?

Чебурашка

  • Hero Member
  • *****
  • Posts: 527
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
But  so-called "Try-Except" syndrome remains.
Exists any better solution?

Also introducing such a sublayer means moving there exception handling. So still you need that as long as you want manage them.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018