2011-08-11 7 views
1

아래의 python에서 단일 노드 컴퓨터 **에서 RSU 메시지가 지원되지 않습니다 **는 인쇄되지 않습니다. 누구든지 도와 드릴까요 ??파이썬 예외 처리

#! /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) 

답변

0

변경에게 마지막 두 줄을에 :

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

나는 여기 단지 Exception를 사용

+0

예외 .__ init __ (self, arg) 이 init 호출에서 arg 인수는 무엇입니까 ?? 그것은 메시지를 인쇄하지 않습니다. – mandeep

+0

메시지를 예외의 일부로 만드는 것이므로 원하는 경우 나중에 직접 인쇄 할 수 있습니다. – agf

+0

좋습니다. 예. 고마워. 정말 도움이되었다. – mandeep

1

try/except 절을 사용하여 예외를 처리하기 때문에 예외가 발생합니다.

try: 
    isPrepActionNeeded() 
except RSUNotSupported as e: 
    print str(e) 
    sys.exit(1) 
2

것은 그것은 당신도 그것을 인쇄 할 :) 여기에 노력하지 않고 있기 때문에, 인쇄되지 않습니다 이것을 베어 키 except:과 동일하게 유지하십시오. 다른 유형의 오류를 숨기지 않으려면 RSUNotSupported을 사용해야합니다.

+0

예외 .__ init __ (self, arg) 여기서 전달 된 arg. 저건 뭘위한거야?? – mandeep

+0

음, 어딘가에 "** RSU ..."메시지를 보관해야합니까? 예외는 Exception 클래스에서 상속되므로 예외 .__ init __ (self, message)는 수퍼 클래스를 초기화하고 인수 (메시지)를 전달합니다. 이제 예외에 해당 메시지가 포함되고 RSUNotSupported 클래스 인스턴스 (또는 해당 인스턴스에서 str())의 __str __()을 호출하면 메시지가 반환됩니다. –