Raising exceptions in OOo Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
vhdlboy
Posts: 3
Joined: Sat Jan 19, 2008 7:27 pm

Raising exceptions in OOo Basic

Post by vhdlboy »

Hi all,

I'm just migrating to OOo basic from VB6 and VBA. I can't find an explanation of how to raise errors. For example, suppose I have some code like the example below, how do generate an error from within the "do something" block?

Apologies if there's a more elegant method for error handling that I've not discovered yet. Please feel free to enlighten me.

Thanks

Code: Select all

Sub Example

   On Error Goto ErrorHandler

   '  do something

   Exit Sub

ErrorHandler:
   
   ' handle error
   
End Sub
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: raising exceptions in OOo basic

Post by JohnV »

Either of these will raise one.

Code: Select all

a=1/0
Dim b as Trash
vhdlboy
Posts: 3
Joined: Sat Jan 19, 2008 7:27 pm

Re: raising exceptions in OOo basic

Post by vhdlboy »

Thanks, but what I meant was how do I raise a user-defined exception, not how do I cause an existing one.
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: raising exceptions in OOo basic

Post by JohnV »

OK, I have never used VB6 or VBA. You may be able to define an error in either but AFAIK you can't in OOo Basic. It does have an Error() function that will raise an internal error by its error number.

Would you give an example of what you might want to define as an error.
vhdlboy
Posts: 3
Joined: Sat Jan 19, 2008 7:27 pm

Re: raising exceptions in OOo basic

Post by vhdlboy »

OK, suppose I want to extend the defined set of run-time errors with a new one that I want to handle in a different way. What I'd expect to find is something that tells me what error codes I can use (the predefined ones are listed in Error Function [Runtime] in the programming help) and for there to be a statement or method along the lines of:

Err.Raise number, source, description

This would generate an error in the same way any other run-time error would, and I could then handle the error at the appropriate level. This is the way VBA does it, and I'm not claiming it's that great, but it's frustrating not to be able to find an alternative.
nickGiard
Posts: 16
Joined: Fri Nov 10, 2017 5:57 pm

Re: raising exceptions in OOo basic

Post by nickGiard »

I think a workaround: Err is Global, why not define globals variable Global globNumber%, Global globDescription$, and before you call 'Dim b as Trash' define that globals, than you can read it in all position in your code in the error handler.
LibreOffice 6.3 on Windows 10 64bit
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: raising exceptions in OOo basic

Post by JeJe »

See Error handling in Andrew Pitonyak's OpenOffice.org Macros Explained. CVErr may be what you want?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
nickGiard
Posts: 16
Joined: Fri Nov 10, 2017 5:57 pm

Re: Raising exceptions in OOo basic

Post by nickGiard »

Thank you JeJe, I have forgotten CVErr , also Pitonyak says "Use CVErr (as mentioned in Table 23) to create an OOo specific internal data type that represents an error. I have never seen this done".
The problem is CVErr create an type error, but doesn't raise the exception. If you don't raise exception, you don't go to the error handler and so it is not useful to came back. Do you know how raise exceprion with CVErr?
LibreOffice 6.3 on Windows 10 64bit
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Raising exceptions in OOo basic

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Raising exceptions in OOo basic

Post by JeJe »

Using VBA's Err object by putting Option VBASupport 1 at the top of the module as per Villeroy's link works in OO too.

nickGiard - looks like you can just use CVErr as a return for a function not raise an error with it.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
nickGiard
Posts: 16
Joined: Fri Nov 10, 2017 5:57 pm

Re: Raising exceptions in OOo basic

Post by nickGiard »

Thank you very much JeJe and Villeroy, very clear example.
I don't know why, but 5 years ago at the start of writing my 20.000 rows of code of my app Edil_LO, I decided do not use Option Compatible or Option VBASupport 1, and code only with original Basic. Maybe I thinked that was more reliability. Is it wrong ??
LibreOffice 6.3 on Windows 10 64bit
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Raising exceptions in OOo basic

Post by JeJe »

nickGiard - don't know whether the VBA compatible code is any more or less bug ridden in OO or LO than the other code but you can do things like class modules with VBA support enabled which can be very useful.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked