Ask LibreOffice - RSS feedhttps://ask.libreoffice.org/en/questions/Questions and answers for LibreOfficeenSun, 17 Feb 2013 23:56:43 +0100How to import UNO exceptions?https://ask.libreoffice.org/en/question/12384/how-to-import-uno-exceptions/I'm working on a Python application that uses a running LO instance. I'd like to know how to import the LO specific exceptions that may occur when running my program. Background below, but the prompting motivation for this question is that I want to catch the specific LO error "NoConnectException". How do I import this error from UNO? No iteration of "`from uno import ...`" I've tried seems to work.
Background:
When a program exception occurs that is actionable, the "Pythonic way" is to be as specific about handling it as possible. For instance, if I have a list of 5 elements, and I try to select the 6th one, that is more specifically an `IndexError`, rather than just a general `Exception`. Thus, instead of code like this:
my_list = [1, 2, 3, 4, 5] # indexed from 0 through 4
i = 5
try:
num = my_list[i]
except Exception as e:
print("You insensitive clod! {} is not a valid index!").format( i )
it is better to use the specific error you intend to handle, thus preventing the masking of an *actual* error:
my_list = [1, 2, 3, 4, 5] # indexed from 0 through 4
i = 5
try:
num = my_list[5]
except IndexError as e:
print("You insensitive clod! {} is not a valid index!").format( i )
How do I import the LO specific errors that can occur? I've tried a number of iterations, but I don't know where to look these up and [api.libreoffice.org](http://api.libreoffice.org/) is not (currently) proving much help to me.Sun, 17 Feb 2013 23:50:14 +0100https://ask.libreoffice.org/en/question/12384/how-to-import-uno-exceptions/Answer by hunteke for <p>I'm working on a Python application that uses a running LO instance. I'd like to know how to import the LO specific exceptions that may occur when running my program. Background below, but the prompting motivation for this question is that I want to catch the specific LO error "NoConnectException". How do I import this error from UNO? No iteration of "<code>from uno import ...</code>" I've tried seems to work.</p>
<p>Background:</p>
<p>When a program exception occurs that is actionable, the "Pythonic way" is to be as specific about handling it as possible. For instance, if I have a list of 5 elements, and I try to select the 6th one, that is more specifically an <code>IndexError</code>, rather than just a general <code>Exception</code>. Thus, instead of code like this:</p>
<pre><code>my_list = [1, 2, 3, 4, 5] # indexed from 0 through 4
i = 5
try:
num = my_list[i]
except Exception as e:
print("You insensitive clod! {} is not a valid index!").format( i )
</code></pre>
<p>it is better to use the specific error you intend to handle, thus preventing the masking of an <em>actual</em> error:</p>
<pre><code>my_list = [1, 2, 3, 4, 5] # indexed from 0 through 4
i = 5
try:
num = my_list[5]
except IndexError as e:
print("You insensitive clod! {} is not a valid index!").format( i )
</code></pre>
<p>How do I import the LO specific errors that can occur? I've tried a number of iterations, but I don't know where to look these up and <a href="http://api.libreoffice.org/">api.libreoffice.org</a> is not (currently) proving much help to me.</p>
https://ask.libreoffice.org/en/question/12384/how-to-import-uno-exceptions/?answer=12386#post-id-12386Sigh, that's embarrassing. Not 5 minutes after posting this, I ran across the answer. (Why couldn't I have found this *before* I posted. Oh well.)
The answer is to first import uno, and then import from com:
import uno
from com.sun.star.connection import NoConnectExceptionSun, 17 Feb 2013 23:56:43 +0100https://ask.libreoffice.org/en/question/12384/how-to-import-uno-exceptions/?answer=12386#post-id-12386