[Lazarus] try..except doesn't trap exception in Form.Showmodal

Luca Olivetti luca at wetron.es
Fri Dec 13 15:37:12 CET 2019


El 13/12/19 a les 15:10, Michael Van Canneyt via lazarus ha escrit:
> 
> 
> On Fri, 13 Dec 2019, Luca Olivetti via lazarus wrote:
> 
>>>
>>> Because ShowModal must run the application message loop, and that 
>>> already catches the
>>> exceptions.  This is in line with the event-based architecture. It's 
>>> the same in Delphi.
>>
>> Yes, I would expect that with Show, but ShowModal is blocking, so I 
>> would expect the try..except to work.
> 
> Yes, but ShowModal is not actually "blocking".
> 
> It fakes this 'blocking' by running the message loop (which is 
> asynchronous by nature).
> If an exception occurs, it is in an event handler, which, from the
> application point of view, is asynchronous.
> 
> It's more like threads. Exceptions in threads are also not propagated to 
> the
> main thread.
> 
> At least, that is how I see it. Not 100% accurate, but it does the trick 
> for my understanding ;-)

Yes, I know all of that, but the message loop could also fake re-raising 
the exception if it was raised in the context of a ShowModal (though I 
reckon that if it did that it would probably break existing 
applications, or maybe not, since the exception would be finally caught 
by the application handler anyway).

BTW, is there a way to know where the exception came from? I don't see 
any method in Exception to do that, and that's the only parameter that 
OnException sees (Sender is nil when I raise an exception inside a form).
My use case is, I want to manage only the exceptions generated in a 
specific form while I'm modal showing it.
I usually set a field telling me that the form is visible and use that 
in OnException to guess where it came from, but that will obviously fail 
if the form is showing and there's an exception generated somewhere else.

Bye
-- 
Luca Olivetti
Wetron Automation Technology http://www.wetron.es/
Tel. +34 93 5883004 (Ext.3010)  Fax +34 93 5883007


More information about the lazarus mailing list