1

I have created a QGIS plugin and I use this code to get my layers by name from layer panel on QGIS.

Here the simple code :

layer1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")[0]
layer2= QgsMapLayerRegistry.instance().mapLayersByName("layer2")[0]
layer3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")[0]

But if I miss some layers from this three layers then my plugin crashed error out of index because can not find the miss layer.

How to create a condition to stop crashed every time to miss some layer and just show me a simple message for ex :layer1 miss from layer panel ?

  • 2
    try: except: docs.python.org/3/tutorial/errors.html would be the simplest way. In your except block you could use sys.exit(Value), where Value is any number but usually negative to indicate an error code, to ungracefully but immediately bail out of execution otherwise the script should just keep running from the next line. – Michael Stimson Jun 6 '17 at 21:29
  • 1
    @MichaelMiles-Stimson - Think you should post that as an answer and probably use iface.messageBar().pushWarning( u'ERROR: ', 'layer(x) missing from layer panel' ) in the except block to show up a warning message if a layer doesn't match :) – Joseph Jun 7 '17 at 10:13
  • @Joseph yes that good but first maybe I need some condition to say something like this if this layer name is not in qgis then iface.messageBar().pushWarning( u'ERROR: ', 'layer(x) missing from layer panel' ) ? – Mar Jun 7 '17 at 13:58
  • 1
    @Joseph, try: except: is general python which is off-topic for this site but iface.messageBar... is QGIS specific which makes it on-topic for this site. I think you have more of an answer in your comment than I. If you have been able to solve this problem Mar based on the comments please consider answering your own question, with a working code block, for the benefit of future users. – Michael Stimson Jun 7 '17 at 21:06
1

To bypass the error, I suggest you to do to best practices. 1. Before you try to access value using an index from a list, check the length of the list. If the list is empty, you will see a stack trace named IndexError. Doing the following will prevent it.

    layers1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")
    layers2= QgsMapLayerRegistry.instance().mapLayersByName("layer2")
    layers3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")

    if len(layers1) > 0:
        pass
        #do stuff
        layer1 = layers1[0]

    if len(layers2) > 0:
        pass
        layer2 = layers2[0]
        #do stuff
    if len(layers3) > 0:
        pass
        layer3 = layers3[0]
        #do stuff
  1. You can also escape the IndexError but I do not see the need if you can use the above code.

    try:
        layer1 = QgsMapLayerRegistry.instance().mapLayersByName("layer1")[0]
        layer2= QgsMapLayerRegistry.instance().mapLayersByName("layer2")[0]
        layer3 = QgsMapLayerRegistry.instance().mapLayersByName("layer3")[0]
    except IndexError as ex:
        print ex
        # send warning...
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.