mandeep
mandeep

Reputation: 283

python exception handling

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)

Upvotes: 1

Views: 345

Answers (3)

agf
agf

Reputation: 176980

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.

Upvotes: 0

Zaur Nasibov
Zaur Nasibov

Reputation: 22679

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)

Upvotes: 2

wRAR
wRAR

Reputation: 25569

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

Upvotes: 1

Related Questions