Sunday, August 24, 2014

Errors and Exceptions in Jython

Welcome to Jython4wsadmin blog!! This post is intended to collect all Errors and Exceptions that can occur when you work on WebSphere administration using wsadmin scripting. As you are aware that Jython is inherited from Python. We can categories the Errors and Exceptions as per their inheriting languages.

  1. Errors defined in Python
  2. Exception from Java
  3. Script based Exceptions depends on WASXShell


When you start woking on the WebSphere Admin scripts you might encounter many types of Errors and Exceptions. Here you must focus on Python based Errors to make syntatic correct code.



wsadmin>execfile('/home/krish/jython/while.py')
WASX7015E: Exception running command: "execfile('/home/krish/jython/while.py')"; exception information:
com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
  File "/home/krish/jython/while.py", line 11
        while x>=0
                  ^
SyntaxError: invalid syntax

colon missing
Here it is simple syntax error  where its fix is appending the colon for the while loop. Same could be possible error in function definitions class definitions, if-else or for loop program controls. So you need to double check the code syntax is structured in right format or not.


wsadmin>execfile('/home/krish/jython/while.py')
WASX7015E: Exception running command: "execfile('/home/krish/jython/while.py')"; exception information:
com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
  File "/home/krish/jython/while.py", line 12
        print x
        ^
SyntaxError: invalid syntax


invalid indundation problem corrected with space.


Exception Handling in Jython


Lets see the simple interactive try-except sample in wsadmin shell. Here I am trying to access the method which does not exist in the AdminTask object.

wsadmin>try: AdminTask.test()
wsadmin>except: print 'catch exception'
wsadmin>
catch exception

Handling with sys.exc_info() dictionary

Sample Exception handling script that uses except block with the sys.exc_info() method which returns a dictionary. You can collect the necessary information out of it using list slice and using dictionary key, value pair of second element.
# Illustration of Simple Exception example

try:
        cellname=AdminControl.getCell()
        print "CellName: ", cellname
        print AdminConfig.list(Cell)
except Exception:
        print "in the Exception block..."
        (k, v)=sys.exc_info()[:2]
        print "Type :", k, "\n", "error reason :", v


Execution of the above script gives the following output:
wsadmin>execfile('/home/vagrant/scripts/except1.py')
CellName:  ubuntu-was8Node01Cell
in the Exception block...
Type : exceptions.NameError
error reason : Cell

The pass statement


This 'pass' statement could be used in the initial state of script. It is going to tell 'DO NOTHING' block. Normally it would be used in the except block.
try:
 # do here some task

except:
 pass 


Raise Exception


The raise keyword is used to propagate a exception when you are using multiple functional calls. Final calling function need the exception details then raise will be used in the except block of called function.
try:
 # Do wsadmin task here

except Exception, e:
 raise "Error Message"


Template for Exception handling


If you keep this template you can build robust script very easily.
try:
 # Do some wsadmin statement execution here

except Exception, e:
 print "Exception catch..."
finally:
 print "In the finally..."
 # Do something 

Please share your comment while you execute these samples on your project automations.

No comments:

Post a Comment

Containerization with Docker

Containerization with Docker
Learn modern microservices containers grow