Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2019-12

try..except doesn't trap exception in Form.Showmodal

11 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

Hello,

I don't remember if I asked this previously, but it always bugs me.

If I do

try
    AForm.ShowModal
except
....
end;

the exceptions eventually raised in AForm aren't caught by the above try..except, they always show the default error dialog. I have to use Application.OnException to catch those. Why?

Bye

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

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 18 lines
Hello, I don't remember if I asked this previously, but it always bugs me. If I do try AForm.ShowModal except .... end; the exceptions eventually raised in AForm aren't caught by the above try..except, they always show the default error dialog. I have to use Application.OnException to catch those. Why?

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.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 13/12/19 a les 10:38, Michael Van Canneyt via lazarus ha escrit:

Show quoted text 27 lines
On Fri, 13 Dec 2019, Luca Olivetti via lazarus wrote: > Hello, > > I don't remember if I asked this previously, but it always bugs me. > > If I do > > > try >    AForm.ShowModal > except > .... > end; > > > the exceptions eventually raised in AForm aren't caught by the above > try..except, they always show the default error dialog. > I have to use Application.OnException to catch those. > Why? 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.
I know it doesn't, but I always forget it :-(
Regarding delphi, I stopped using it ten years ago. more or less ;-)

Bye

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

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 8 lines
> > 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 ;-)

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 13/12/19 a les 15:10, Michael Van Canneyt via lazarus ha escrit:

Show quoted text 26 lines
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

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 8 lines
> 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).

Not correct.

I have code
   if ShowModal=mrOK then
     begin
     end
   else
    begin
    end
No matter how many exceptions are raised, the code will always go through
either block of the if then statement.

If ShowModal would propagate this, then neither of the code blocks would be executed.

So changing this is a no-no.

Show quoted text 9 lines
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.

Why not use Screen.ActiveForm ?

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 13/12/19 a les 15:51, Michael Van Canneyt via lazarus ha escrit:

Show quoted text 11 lines
> 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. Why not use Screen.ActiveForm ?

Never thought of that, d'ho!
But then it has the same problem: even if the form is active, the exception could come from somewhere else.

Bye

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

Reply by email

Luca Olivetti
to Mattias Gaertner

El 13/12/19 a les 16:41, Mattias Gaertner via lazarus ha escrit:

Show quoted text 8 lines
On Fri, 13 Dec 2019 16:24:26 +0100 Luca Olivetti via lazarus wrote: > [...] > But then it has the same problem: even if the form is active, the > exception could come from somewhere else. Do you want the call stack?

I know how to get the call stack, it's in the wiki and I use it in some applications to log the unhandled exceptions to a file for later debug, but I don't know if and how I can determine if the exception was generated in a specific form.

Bye

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

Reply by email

Mattias Gaertner
to Luca Olivetti
Show quoted text 14 lines
El 13/12/19 a les 16:41, Mattias Gaertner via lazarus ha escrit: > On Fri, 13 Dec 2019 16:24:26 +0100 > Luca Olivetti via lazarus wrote: > >> [...] >> But then it has the same problem: even if the form is active, the >> exception could come from somewhere else. > > Do you want the call stack? I know how to get the call stack, it's in the wiki and I use it in some applications to log the unhandled exceptions to a file for later debug, but I don't know if and how I can determine if the exception was generated in a specific form.

As you know, not every exception is raised in a specific form. Maybe you can give some example(s), what you want to achieve?

Mattias

Reply by email

Luca Olivetti
to Mattias Gaertner

El 13/12/19 a les 17:10, Mattias Gaertner via lazarus ha escrit:

Show quoted text 20 lines
On Fri, 13 Dec 2019 16:52:36 +0100 Luca Olivetti via lazarus wrote: > El 13/12/19 a les 16:41, Mattias Gaertner via lazarus ha escrit: >> On Fri, 13 Dec 2019 16:24:26 +0100 >> Luca Olivetti via lazarus wrote: >> >>> [...] >>> But then it has the same problem: even if the form is active, the >>> exception could come from somewhere else. >> >> Do you want the call stack? > > I know how to get the call stack, it's in the wiki and I use it in > some applications to log the unhandled exceptions to a file for later > debug, but I don't know if and how I can determine if the exception > was generated in a specific form. As you know, not every exception is raised in a specific form. Maybe you can give some example(s), what you want to achieve?

I gave it in my first example:

try
   AForm.Showmodal;
except
   ...
end

And since that isn't supposed to work, I have to trap it with Application.OnException.

My use case is an unattended application, where every uncaught exception should abort/restart it, *unless* the exception is generated in an interactive form (i.e, a user is there, watching it), in that case I can simply show a message tailored to the form that caused the exception.

Bye

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

Reply by email