1

In the below python the message RSU is not supported on single node machine** is not getting printed. can anyone help please??

#! /usr/bin/env python

import sys

class SWMException(Exception):
    def __init__(self, arg):
        print "inside exception"
        Exception.__init__(self, arg)

class RSUNotSupported(SWMException):
    def __init__(self):
        SWMException.__init__(self, "**RSU is not supported on single node machine**")

def isPrepActionNeeded():
    if 1==1:
        raise RSUNotSupported()
try:
    isPrepActionNeeded()
except:
    sys.exit(1)

3 Answers 3

2

It is not printed, because you're even not trying to print it :) Here:

try:
    isPrepActionNeeded()
except RSUNotSupported as e:
    print str(e)
    sys.exit(1)
Sign up to request clarification or add additional context in comments.

2 Comments

Exception.__init__(self, arg) the arg passed here. what is that for??
Well, you do have to keep the "**RSU..." message somewhere, right? Your exception inherits from Exception class, thus Exception.__init__(self, message) initializes the superclass and passes the argument (the message) to it. Now, your exception contains that message, and calling __str__() of an RSUNotSupported class instance (or str( ) on that instance) returns the message.
1

Because you handle the exception with your try/except clause.

Comments

0

Change the last two lines to:

except Exception as e:
    print e
    sys.exit(1)

I use just Exception here to keep this the equivalent of a bare except:. You really should use RSUNotSupported so you don't hide other types of errors.

2 Comments

Exception.__init__(self, arg) what is the arg argument in this init call for?? it doesnt print the message
It's to make the message part of the exception, so you can print it yourself later if you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.