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)
asked Aug 11, 2011 at 7:14
mandeep
4311 gold badge9 silver badges17 bronze badges
3 Answers 3
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)
answered Aug 11, 2011 at 7:18
Zaur Nasibov
22.7k12 gold badges61 silver badges86 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
mandeep
Exception.__init__(self, arg) the arg passed here. what is that for??
Zaur Nasibov
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.
Because you handle the exception with your try/except clause.
answered Aug 11, 2011 at 7:17
wRAR
25.7k5 gold badges89 silver badges101 bronze badges
Comments
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.
answered Aug 11, 2011 at 7:18
agf
178k45 gold badges300 silver badges241 bronze badges
lang-py