2016-05-14 62 views
0

我需要一點點幫助理解,我有使用Python 2.7.11用戶定義的異常問題。Python的用戶定義的異常處理

我有兩個文件main.pymyErrors.py。在主要我發佈的數據和接收響應,並在myErrors我處理這些錯誤。

我想要做的是在嘗試執行版本錯誤:除了語句,但甚至認爲它應該是它沒有得到執行。我在做什麼是我傳給myErrors和更新數據字典中的錯誤file.-

我的問題是嚴重的措辭迴應。我想要做的是,將響應傳遞給錯誤處理程序,但我不想執行它,直到我們遇到on_response方法中的Try:except子句。所以當我們得到響應並且不成功時,請檢查錯誤代碼並引發異常。現在我在做什麼,首先檢查錯誤,然後執行校驗成功(錯誤代碼)

這裏是主要

def send_messages(self): 
    response = cm.postData(url=simulateSasServer, jsondata=json_data) 
    self.on_response(response) 

def on_response(self, response): 
myERRORS.myERRORS(response) 
    # if registration is succesful change state to REGISTERED. 
    if 'registrationResponse' in response: 
     try: 
      responseObjects = response['registrationResponse'] 
      for responseObject in responseObjects: 
       if responseObject['error']['errorCode'] == 0: 
        do_action 
     except myErrors.Version(): 
      raise ("version_message") 

這裏是myErrors

class myERRORS(Exception): 

error_code = {'SUCCESS': 0, 
       'VERSION': 100, 
       } 

response_data = {} 

def __init__(self, response): 
    self.response_data.update(response) 



class Version(myERRORS): 

def __init__(self): 
    self.name = "VERSION" 
    self.err_code = self.error_code['VERSION'] 
    self.msg = "SAS protocol version used by CBSD is not supported by SAS" 
    self.version_error() 
    if self.version_error() is True: 
     print (self.name, self.err_code, self.msg) 
     raise Exception(self.name, self.err_code, self.msg) 

def version_error(self): 
    response_objects = self.response_data.values()[0] 
    if 'registrationResponse' in self.response_data: 
     for r_object in response_objects: 
      if r_object['error']['errorCode'] == self.error_code['VERSION']: 
       return True 

任何幫助,非常感謝。

回答

0

是不是真的有什麼特別的異常。他們是班級。你所做的是創建一個類的實例。你沒有提出。變化:

myERRORS.myERRORS(response) 

到:

raise myERRORS.myERRORS(response) 
+0

是啊,我想我的問題是嚴重的措辭。 我想要做的是爲傳遞到錯誤處理程序的響應,但我不希望執行它,直到我們得到了嘗試:除了條款。 所以,當我們得到的迴應,如果它不成功,則檢查錯誤代碼並引發異常。 現在我正在做的是先檢查錯誤,然後執行成功檢查(錯誤代碼) –