Maximo

Maximo

Come for answers, stay for best practices. All we're missing is you.

 View Only
  • 1.  Unable to catch java exceptions in automation script

    Posted Mon July 21, 2025 10:39 AM

    I have an automation script that I am working on that implements a REST API request. I'm having a problem trapping exceptions coming from java method calls. In this case, I am calling the add() method on an mbo opened on the PLUSDESTVERSION table. There is a custom java validation that throws an exception if the value assigned to the WORKGROUP field isn't a valid record. I have added multiple levels of "try/except" blocks around the add() call, but the except blocks aren't getting invoked, and my REST API call is returning a 400 status, with the response JSON body set to the data from the java error.

    Here is the code:

                try:
                    version = versionSet.add()
                    version.setValue("CONTRIBUTIONMETHOD", "N")
                    version.setValue("APPLYCONTRIBUTION", "N")
                    log("Before setValue(WORKGROUP)")
                    try:
                        version.setValue("WORKGROUP", parentWO.getString("OWNERGROUP"))
                    except Exception as addEx:
                        log_error("EXCEPTION during version.setValue(): {}".format(str(addEx)))
                        raise Exception("EXCEPTION during version.setValue(): {}".format(str(addEx)))
                    
                    log("After setValue(WORKGROUP)")
                    version.setValue("OVERHEADTYPE", parentWO.getString("OPSSITEID"))
                    if ogsLvInitiatedCue == "Y":
                        version.setValue("STORELOC", storeLoc)
                        if opsSiteId == "ONG":
                            # Set default for ONG only
                            log("Setting PLUSDESTVERSION.OGSREIMBURSABLE for ONG")
                            version.setValue("OGSREIMBURSABLE", "No")
                        
                    versionSet.save()
                except Exception as addEx:
                    log_error("EXCEPTION during versionSet.add(): {}".format(str(addEx)))
                    raise Exception("EXCEPTION during versionSet.add(): {}".format(str(addEx)))

    Rather than my exception message appearing in the REST call's response body, I'm getting this:

    {
        "Error": {
            "errorattrname""workgroup",
            "extendedError": {
                "moreInfo": {
                    "href""http://localhost/maximo/api/error/messages/BMXAP0395E"
                }
            },
            "correlationid"null,
            "errattrvalue"null,
            "reasonCode""BMXAP0395E",
            "message""BMXAP0395E - Person Group OTRE is not a valid TnD Work Group.",
            "statusCode""400"
        }
    }
    My log messages are also not getting written to SystemOut.log.

    Is what I am doing correct? Or are automation scripts unable to trap exceptions from calls to IBM java classes?


    ------------------------------
    Theo Pozzy
    ------------------------------


  • 2.  RE: Unable to catch java exceptions in automation script

    Posted Tue July 22, 2025 10:14 AM

    Java and Python both have an Exception class. What your code is catching is the Python Exception.

    To resolve this conflict, import the Java Exception with an alias, like this:

    from java.lang import Exception as JavaException

    Then, have an except clause for each of Exception and JavaException.



    ------------------------------
    Blessings,
    Jason Uppenborn
    Sr. Technical Maximo Consultant
    Cohesive
    ------------------------------



  • 3.  RE: Unable to catch java exceptions in automation script

    Posted Tue July 22, 2025 10:35 AM

    Thank you for the information about the Java exception class. Can you share an example that shows how to use the class around a Java method call?

    Thank you!



    ------------------------------
    Theo Pozzy
    ------------------------------



  • 4.  RE: Unable to catch java exceptions in automation script

    Posted Tue July 22, 2025 10:59 AM
    Edited by Jason Uppenborn Wed July 23, 2025 10:02 AM

    When I need to catch a Java exception, it often looks like this:

    from java.lang import Exception as JavaException
    logger = mbo.getMboLogger()
    try:
        logger.debug("Do something that could throw an exception")
    except Exception as e:
        logger.error("Python Exception: {}".format(str(e)))
    except JavaException as e:
        logger.error("Java Exception: {}".format(e.getMessage()), e)



    ------------------------------
    Blessings,
    Jason Uppenborn
    Sr. Technical Maximo Consultant
    Cohesive
    ------------------------------



  • 5.  RE: Unable to catch java exceptions in automation script

    Posted Wed July 23, 2025 08:50 AM

    When catching JavaExceptions, i often tend to use Throwable instead of Exception. Some external libs extends its Exceptions from it.

    So i end up more with this:

    from java.lang import Throwable
    logger = mbo.getMboLogger()
    try:
        logger.debug("Do something that could throw an exception")
    except Exception as e:
        logger.error("Python Exception: {}", str(e))
    except Throwable as e:
        logger.error("Java Exception: {}".format(e.getMessage()), e)



    ------------------------------
    Andreas Brieke
    IT Service Management Consultant
    SVA System Vertrieb Alexander GmbH
    ------------------------------