Handling errors in datamodule lazarus / 16-Databases ---------------------------------------------------------------------- anis2505 · 2015-12-24 11:29 Hi to all , I have a TDatamodule component. All my data access component( sqlquery, datasource, ... ) are in the datamodule. I have a form with  dbgrid and a dbnav. So WITHIN THE FORM I need to catch errors raised by the data access component located in the datamodule I hope this is clear. Please any idea would be great regards GH. Anis ---------------------------------------------------------------------- molly · 2015-12-24 12:26 Quote from: anis2505 on December 24, 2015, 12:29:23 pmI hope this is clear. Not entirely  The datamodule is part of your application (see your project source- code where the datamodule is added to the application), and the form is the one using the components. Your typical try except block would be able to catch errors. Isn't that working for you ? In case you need earlier 'interception' you could always opt for the application.OnException event ? ---------------------------------------------------------------------- anis2505 · 2015-12-24 12:51 Hi, Simply I need to catch the  errors of the sqlquery which is in the datamodule in the form so I can show a messagebox or so ---------------------------------------------------------------------- molly · 2015-12-24 13:07 How do you 'invoke' that query exactly ? If you do that manually then you can put a try .. except block around the code that does the query (something like shown here, although not query related). See here (it was right below the previous link i posted earlier :-S) In case the query is executed automatically (on component creation) then you would need to add an exception handler to the application. You can do that inside your datamodule.oncreate (that should be able to catch it, if not add the event earlier before module creation, e.g. when the form is created.). In case the latter, i strongly suggest invoking the query outside automatic creation, as that offers more control with try..except (and/or finally) blocks. ---------------------------------------------------------------------- mangakissa · 2015-12-24 16:00 Like Molly said. You can also use the OnPostError event for creating a messagebox. Or redirect your OnPostError to a precedure in your form. But let the datamodule work for these things. It's no shame to put a dialog.pas in your uses of datamodule. If you using try..except you can raise an error like showmessage by using on  E: EDatabaseError do. Therefore pialog.pas is not needed.