how to skip missing image with getADDEImage()

Post any questions, ideas, or topics related to Jython and Python scripting.
Post Reply
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

how to skip missing image with getADDEImage()

Post by hproe »

Hi -

When getADDEImage() hits on a missing image, image retrieving stops in an uncontrollable way within getADDEImage(). The traceback points to a failure at the code line

Code: Select all

area = AreaAdapter(url)

Is there a way to skip missing images and keep going?

HP
User avatar
mhiley
Posts: 90
Joined: Mon Jan 23, 2012 10:22 pm

Re: how to skip missing image with getADDEImage()

Post by mhiley »

Hi HP,

If getADDEImage is failing and you want to continue the script, you should be able to accomplish this with Python "try/except" statements. The simplest form is something like this:

Code: Select all

try:
    # code that could potentially result in error
    getADDEImage(**parms)
except Exception:
    # here, define whatever logic you want to handle getADDEImage failure
    # for now, just print a message and continue
    print 'Something went wrong in getADDEImage'

print 'This code should run even if getADDEImage failed'

More complete information on "try/except" can be found in the Python documentation, especially section "8.3 Errors and Exceptions":
http://docs.python.org/2.5/tut/node10.html

Finally, could you post the full error message/traceback and McV version number/build date just to make sure I'm understanding the problem correctly?

-Mike
User avatar
hproe
Posts: 504
Joined: Sat Nov 27, 2010 3:46 pm

Re: how to skip missing image with getADDEImage()

Post by hproe »

Hi Mike -

I've got it working - many thanks for the hint. However, the code works only with a wildcard except: clause. Both 'AddeURLException' or 'Exception' fail to catch the exception. In the attached file you find the code (with wildcard except) and a traceback when the catch fails (McV 1.3beta1 build 2013-05-24 23:15, Win7 64bit).

BTW getADDEImage still prints the URL and some further data for each call. I have not found a way to suppress this printout (in the shell). It is certainly helpful when debugging, but it spoils useful output with working code.

HP
Attachments
getADDEImage_Exception.txt
(2.65 KiB) Downloaded 348 times
User avatar
mhiley
Posts: 90
Joined: Mon Jan 23, 2012 10:22 pm

Re: how to skip missing image with getADDEImage()

Post by mhiley »

Hi HP,
Sorry about that, you're exactly right. I'm glad you got it working despite my example!

I don't think there's any need to change your code, but if you do want to explicitly catch only the VisADException thrown by getADDEImage, here's how it looks:

Code: Select all

from visad import VisADException
try:
    getADDEImage(**parms)
except VisADException, exc:
    # define code to handle the exception
    print exc

(The problem with my original example is that exceptions resulting from Java code do not extend Jython's "Exception" class, so in our McV environment, the 'except Exception:' statement is not a valid way to catch all exceptions. I shouldn't post examples without testing them!!)

-Mike
Post Reply