python - 仅打印一个单词的异常

标签 python exception

我有一个类,如下所示:

def set_new_mode(self,mode):
    try:
        #this will fail, since self.keithley is never initialized
        print self.keithley
        self.keithley.setzerocheck(on=True)
        self.keithley.selectmode(mode,nplc=6)
        self.keithley.setzerocheck(on=False) #keithcontrol class will 
        #automatically turn on zero correction when zchk is disabled
        self.mode = mode
        self.print_to_log('\nMode set to %s' % self.mode)
    except Exception as e:
        self.print_to_log('\nERROR:set_new_mode: %s' % e)
        print e

作为一些错误处理测试的一部分,我尝试调用 set_new_mode 函数,而无需首先初始化类变量 self.keithley。在这种情况下,我希望 print self.keithley 语句会引发 AttributeError: keithgui 实例没有属性 'keithley'。但是,print eself.print_to_log('\nERROR:set_new_mode: %s' % e) 表明 e 仅包含单词“吉时利”。

print e 更改为 print type(e) 显示 e 仍然具有 AttributeError 类型,但该变量不再包含任何有用的内容有关异常的信息。为什么?如何将 e 返回到其预期形式?

编辑:这是一个重现错误的 MEW。 要重现错误,请启动 GUI,将模式更改为 VOLT 以外的模式,然后单击更新按钮。

import Tkinter

import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg


class keithgui(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        #we are not initially connected to the keithley
        self.connected = False
        self.pauseupdate = False

        #set up frames to distribute widgets
        #MASTER FRAME
        self.mframe = Tkinter.Frame(self,bg='green')
        self.mframe.pack(side=Tkinter.TOP,fill='both',expand=True)
        #LEFT AND RIGHT FRAMES
        self.Lframe = Tkinter.Frame(self.mframe,bg='red',borderwidth=2,relief='raised')
        self.Lframe.pack(side='left',fill='both',expand=True)
        self.Rframe = Tkinter.Frame(self.mframe,bg='blue',borderwidth=2,relief='raised')
        self.Rframe.pack(side='right',fill='both',expand=False)

        #create the log text widget to keep track of what we did last
        #also give it a scrollbar...
        scrollbar = Tkinter.Scrollbar(master=self.Lframe)
        scrollbar.pack(side=Tkinter.RIGHT,anchor='n')
        self.logtext = Tkinter.Text(master=self.Lframe,height=3,yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.logtext.yview)
        self.logtext.pack(side=Tkinter.TOP,anchor='w',fill='both')

        #Button to update all settings
        updatebutton = Tkinter.Button(master=self.Rframe,text='Update',command=self.update_all_params)
        updatebutton.grid(column=2,row=0)


        #Option menu & label to select mode of the Keithley
        modes = ['VOLT','CHAR','CURR']
        modelabel = Tkinter.Label(master=self.Rframe,text='Select Mode:')
        modelabel.grid(column=0,row=2,sticky='W') 
        self.mode = 'VOLT'
        self.modevar = Tkinter.StringVar()
        self.modevar.set(self.mode)
        modeselectmenu = Tkinter.OptionMenu(self.Rframe,self.modevar,*modes)
        modeselectmenu.grid(column=1,row=2,sticky='W')

    def print_to_log(self,text,loc=Tkinter.END):
        self.logtext.insert(loc,text)
        self.logtext.see(Tkinter.END)



    def update_all_params(self):
        self.set_refresh_rate()
        if self.modevar.get() != self.mode:
            self.set_new_mode(self.modevar.get())
        else:
            self.print_to_log('\nAlready in mode %s' % self.mode)

    def set_refresh_rate(self):
        try:
            self.refreshrate = np.float(self.refreshrateentryvar.get())
            self.print_to_log('\nRefresh rate set to %06.3fs' % self.refreshrate)
        except Exception as e:
            self.print_to_log('\nERROR:set_referesh_rate: %s' % e)

    def set_new_mode(self,mode):
        try:
            print self.keithley
            self.keithley.setzerocheck(on=True)
            self.keithley.selectmode(mode,nplc=6)
            self.keithley.setzerocheck(on=False) #keithcontrol class will 
            #automatically turn on zero correction when zchk is disabled
            self.mode = mode
            self.print_to_log('\nMode set to %s' % self.mode)
        except Exception as e:
            self.print_to_log('\nERROR:set_new_mode: %s' % e)
            print e
            print type(e)

if __name__ == "__main__":
    app = keithgui(None)
    app.title('Keithley GUI')
    app.mainloop()

最佳答案

如果您修改代码:

import Tkinter as tk

class Fnord(tk.Tk):
    def set_new_mode(self,mode):
        try:
            import pdb; pdb.set_trace()
            #this will fail, since self.keithley is never initialized
            print self.keithley

Fnord().set_new_mode('whatever')

然后开始逐步执​​行 s ,你会看到有一个 __getattr__在你的窗口上运行。我现在正在查找导致问题的原因,但这实际上就是您的答案。

<小时/>

根据调用堆栈,我找到了一个调用 self.tk = _tkinter.create ,最终让我得到 here 。最终这归结为异常发生在 C 区域,因此它产生了不同的 AttributeError消息。

关于python - 仅打印一个单词的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599734/

相关文章:

python - python 迭代美丽汤结果集

python - 按列比较不同的 pandas 数据框与公差变化

python - 指向随机形状的多边形?

抛出 ConnectException 之前的 Java 延迟

java - try catch 将名称拆分为第一个和最后一个的异常

python - 如何引用 Python 对象并将其放入 MySQL?

python - 替换 Pandas 列中的值

java - com.microsoft.sqlserver.jdbc.SQLServerException : The index 2 is out of range :(

Android:致命异常问题:尝试在单击按钮时调用方法

c# - Context.Items 在页面刷新/传输期间清除