Forum > LCL

Using ApplicationProperties.OnException by TApplication.HandleException

(1/1)

rockstein:
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  [+][-]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";}};} ---FApplicationHandlers[ahtException] which executes my events one by one.


--- 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";}};} ---       while FApplicationHandlers[ahtException].NextDownIndex(i) do          TExceptionEvent(FApplicationHandlers[ahtException][i])(Sender, Exception(ExceptObject)); 

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  [+][-]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";}};} ---if E is ESocketError then begin    AddLog ((E as  ESocketError).Msg);  AddLog ((E as  ESocketError).Code);end;if E is EMyCoolExeption then begin    AddLog ((E as  EMyCoolExeption).Msg);  AddLog ((E as  EMyCoolExeption).SomeChildMethod);end; 
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

Чебурашка:
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.

rockstein:
Спасибо за быстрый ответ, чебурашка ;)

--- Quote from: Чебурашка on August 21, 2023, 05:16:13 pm ---you are missing a sublayer where your real operations are done

--- End quote ---
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?

Чебурашка:

--- Quote from: rockstein on August 21, 2023, 05:44:52 pm ---But  so-called "Try-Except" syndrome remains.
Exists any better solution?

--- End quote ---

Also introducing such a sublayer means moving there exception handling. So still you need that as long as you want manage them.

Navigation

[0] Message Index

Go to full version