Forum Discussion

trans-am's avatar
trans-am
Occasional Contributor
2 years ago

Try-Catch "No such property: carried for the class:" error

Following piece of groovy:

 

import groovy.json.JsonSlurper

def response = context.expand( '${GetAccumulated#response}' ).toString()

log.info(response) // This is OK. List is printed to the console

def slurper = new JsonSlurper()

def json = slurper.parseText response

log.info(json)  // OK and printed to the console

 

// then I try to catch exception

try          {

               def carried = json.carriedFromPrevious  // taking carriedFromPrevious from the list

               log.info("carriedFromPrevious = " + carried) // looks OK, so correct number is printed to the console

               }

               catch (Exception ex)

                        {

                        log.info("Respond not a number")

                        log.info(ex)

                        }

log.info("carriedFromPrevious = " + carried) // when trying to print it again comes an error "No such property: carried for the class:"

 

// Now I am wondering what will happen to "carried" variable during try-catch operation? Why I cannot print it again? Do I have to specify it somewhere again? 

 

  • its because you define carried in the try block.

     

    please try to define carried before the try block.

     

    def carried

    try          {

                   carried = json.carriedFromPrevious  

                   log.info("carriedFromPrevious = " + carried) 

                   }

                   catch (Exception ex)

                            {

                            log.info("Respond not a number")

                            log.info(ex)

                            }

    log.info("carriedFromPrevious = " + carried) 

  • Ok, works now. I had a little error in my script.

  • its because you define carried in the try block.

     

    please try to define carried before the try block.

     

    def carried

    try          {

                   carried = json.carriedFromPrevious  

                   log.info("carriedFromPrevious = " + carried) 

                   }

                   catch (Exception ex)

                            {

                            log.info("Respond not a number")

                            log.info(ex)

                            }

    log.info("carriedFromPrevious = " + carried) 

  • trans-am's avatar
    trans-am
    Occasional Contributor

    Actually I already tried it once and  " startup failed: Script4.groovy: 28: The current scope already contains a variable of the name carried @ line 28, column 7. def carried ="  so it doesn't let me define twice.

    • trans-am's avatar
      trans-am
      Occasional Contributor

      Ok, works now. I had a little error in my script.