异常消息输出问题 [英] exception message output problem

查看:76
本文介绍了异常消息输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑为什么我的异常消息没有正确显示




我有一个代表物理标量的类。如果我输入


>> 3 * s + 4 * m



我应该得到这样的东西:


scalar.InconsistentUnits:3 s,4 m


表示秒数无法添加到米。相反,消息

被分解为单个字符,如下所示:


scalar.InconsistentUnits:(''3'','''', ''','','','''',''4'','''','''')


这个工作正常几个月,所以我不明白什么是错误的。我在带有Red Hat的戴尔工作站上使用版本2.5.1

企业WS Linux。


据我所知,我正在做所有事情;通过这本书。这是我的

异常类:


类InconsistentUnits(例外):

def __init __(self,args ="" ;):self.args = args


这里是我的实例:


raise scalar.InconsistentUnits,str(self)+ , + str(其他)


我尝试了各种小改动,但似乎没有任何改变

的差异。有没有人注意到这种行为?谢谢。


顺便说一下,我的标量类可以从 http://RussP.us/scalar.htm

..提供完整的用户指南。看看这个。我想你会喜欢这样的b $ b。

解决方案

12月22日,2:57 * am,Russ P. < Russ.Paie ... @ gmail.comwrote:


我为什么我的异常消息没有正确显示

感到困惑。


我有一个代表带有单位的物理标量的类。如果我输入


> 3 * s + 4 * m



我应该得到这样的东西:


scalar.InconsistentUnits:3 s,4 m


来显示秒数无法添加到米。 *相反,消息

被分解为单个字符,如下所示:


scalar.InconsistentUnits:(''3'','''' ,''','','','''',''4'','''','''')


这个工作正常好几个月了,所以我不明白是什么原因是错误的。我在带有Red Hat的戴尔工作站上使用版本2.5.1

企业WS Linux。


据我所知,我正在做所有事情;通过这本书。这是我的

异常类:


* *类InconsistentUnits(例外):

* * * * def __init __(self ,args =""):self.args = args


这里是我的实例:


* * * * raise scalar.InconsistentUnits,str(self)+"," + str(其他)


我尝试了各种小改动,但似乎没有任何改变

的差异。有没有人注意到这种行为?谢谢。


顺便说一句,我的标量类可以从http://RussP.us/scalar.htm免费获得

。提供完整的用户指南。看看这个。我想你会喜欢它的b $ b。



问题出现在InconsistentUnits异常中。似乎

的行为:

self.args =''一些字符串''

会将字符串分成字符,可能是因为self.args认为

''Some String''是某种元组。


将异常更改为:

class InconsistentUnits(Exception):

def __init __(self,args =""):self.args =(args,)

#Python有奇怪(阅读:破碎)单例实现

#单个成员元组后面必须有逗号


谎言écrit:

(snip)


#Python有一个奇怪的(读取:损坏的)单例实现

#单个成员元组后面必须有一个逗号



如果你愿意,你可以称它为奇怪甚至是疣,但是考虑到什么

使得元组是逗号 - 而不是parens [1] - ,它没有被破坏。


[1]除了空的t uple。


12月21日下午2:58,Lie< Lie.1 ... @ gmail.comwrote:


将异常更改为:

class InconsistentUnits(Exception):

def __init __(self,args =""):self.args =(args,)

#Python有一个奇怪的(读取:损坏的)单例实现

#单个成员元组后面必须有一个逗号


嘿,这很有效。谢谢。


实际上,不需要parens,所以这也有效:


def __init __(self,args =" "):self.args = args,


后面的逗号不需要一段时间(2.5之前?),所以

Python必须改变。我会说它看起来有点

清洁剂没有尾随的逗号,所以也许改变后应该改变回来。

变回来。


I am baffled about why my exception messages are not displaying
properly.

I have a class that represents physical scalars with units. If I type

>>3 * s + 4 * m

I should get something like this:

scalar.InconsistentUnits: 3 s, 4 m

to show that seconds cannot be added to meters. Instead, the message
is broken up into individual characters, like this:

scalar.InconsistentUnits: (''3'', '' '', ''s'', '','', '' '', ''4'', '' '', ''m'')

This worked correctly for months, so I don''t understand what went
wrong. I am using version 2.5.1 on a Dell workstation with Red Hat
Enterprise WS Linux.

As far as I can tell, I am doing everything "by the book." Here is my
exception class:

class InconsistentUnits(Exception):
def __init__(self, args=""): self.args = args

and here is my instantiation of it:

raise scalar.InconsistentUnits, str(self) + ", " + str(other)

I''ve tried various little changes, but nothing seems to make any
difference. Has anyone else noticed this sort of behavior? Thanks.

By the way, my scalar class is available for free from http://RussP.us/scalar.htm
.. A complete user guide is available. Check it out. I think you''ll
like it.

解决方案

On Dec 22, 2:57*am, "Russ P." <Russ.Paie...@gmail.comwrote:

I am baffled about why my exception messages are not displaying
properly.

I have a class that represents physical scalars with units. If I type

>3 * s + 4 * m


I should get something like this:

scalar.InconsistentUnits: 3 s, 4 m

to show that seconds cannot be added to meters. *Instead, the message
is broken up into individual characters, like this:

scalar.InconsistentUnits: (''3'', '' '', ''s'', '','', '' '', ''4'', '' '', ''m'')

This worked correctly for months, so I don''t understand what went
wrong. I am using version 2.5.1 on a Dell workstation with Red Hat
Enterprise WS Linux.

As far as I can tell, I am doing everything "by the book." Here is my
exception class:

* * class InconsistentUnits(Exception):
* * * * def __init__(self, args=""): self.args = args

and here is my instantiation of it:

* * * * raise scalar.InconsistentUnits, str(self) + ", " + str(other)

I''ve tried various little changes, but nothing seems to make any
difference. Has anyone else noticed this sort of behavior? Thanks.

By the way, my scalar class is available for free fromhttp://RussP.us/scalar.htm
. A complete user guide is available. Check it out. I think you''ll
like it.

The problem is in the InconsistentUnits Exception. It seems that the
act of:
self.args = ''Some String''
would break the string into chars, possibly because self.args think
''Some String'' is a tuple of some sort.

Change the exception into this:
class InconsistentUnits(Exception):
def __init__(self, args=""): self.args = (args,)
# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it


Lie a écrit :
(snip)

# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it

You may call it weird or even a wart if you want, but given that what
makes the tuple is the comma - not the parens[1] -, it is _not_ broken.

[1] with the exception of the empty tuple.


On Dec 21, 2:58 pm, Lie <Lie.1...@gmail.comwrote:

Change the exception into this:
class InconsistentUnits(Exception):
def __init__(self, args=""): self.args = (args,)
# Python have an odd (read: broken) singleton implementation
# single member tuple must have a comma behind it

Hey, that worked. Thanks.

Actually, the parens aren''t needed, so this works too:

def __init__(self, args=""): self.args = args,

The trailing comma wasn''t necessary a while back (pre 2.5?), so
something in Python must have changed. I''d say that it looks a bit
cleaner without the trailing comma, so maybe whatever changed should
get changed back.


这篇关于异常消息输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆