最近用webpy開發web應用,不出所料剛開始果真遇到不少未知問題,這裏總結一下。 python
調試模式下異常捕捉問題,在開發過程當中我在操做數據庫是總是遇到如下的這個問題(實際上全部異常都報這個錯誤...) web
File "D:\����\eclipesWork\python\PyTest\src\web\debugerror.py", line 305, in debugerror return web._InternalError(djangoerror()) File "D:\����\eclipesWork\python\PyTest\src\web\debugerror.py", line 295, in djangoerror return t(exception_type, exception_value, frames) File "D:\����\eclipesWork\python\PyTest\src\web\template.py", line 881, in __call__ return BaseTemplate.__call__(self, *a, **kw) File "D:\����\eclipesWork\python\PyTest\src\web\template.py", line 808, in __call__ return self.t(*a, **kw) File "D:\����\eclipesWork\python\PyTest\src\web\debugerror.pyc", line 153, in __template__ <ul class="traceback"> File "D:\����\eclipesWork\python\PyTest\src\web\template.py", line 825, in _escape value = safeunicode(value) File "D:\����\eclipesWork\python\PyTest\src\web\utils.py", line 348, in safeunicode return obj.decode(encoding) File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xb9 in position 3: invalid start byte這樣的問題讓我非常糾結由於我根本看不到具體是什麼問題。解決方式建一個超類捕獲GET與POST的異常.
# -*- coding: utf-8 -*- ''' Created on 2013-11-19 @author: 梁前武 QQ:1587790525 ''' class baseweb(): def myGET(self): return "baseweb get" def myPOST(self): return "baseweb post" def GET(self): v="" try: v=self.myGET() except Exception,e: return "erro %s"%(e) return v def POST(self): v="" try: v=self.myPOST() except Exception,e: return "erro %s"%(e) return v之後全部的操做類都繼承這個超類而且實現對應的myGET和myPOST方法(不用GET和POST方法)
class userreg(baseweb): def myGET(self): ...這樣出現異常之後能夠直接在調試網頁顯示異常信息以下圖