EJB 3.0异常处理(EJB 3.0 exceptions handling)
来自EJB规范的引用:
如果bean方法遇到系统异常或错误,它应该简单地将错误从bean方法传播到容器(即,bean方法不必捕获异常)。
但我不明白。 这是否意味着我不应该捕获所有类型的异常(即尝试捕获
Exception
类)并将其重新抛出为我的应用程序异常?一个更清晰的例子:
public void beanMethod throws MyApplicationException { try { // do something } catch (Exception e) { throw new MyApplicationException(e); // Should I do it like this? } }
或者,这不适用于EJB开发人员,而只适用于EJB引用实现开发人员(容器开发人员):在后一种情况下,容器不得将系统异常传播到我的业务方法,并且我的
catch(Exception e)
块从来没有捕捉到任何系统异常?A quote from the EJB specification:
If the bean method encounters a system exception or error, it should simply propagate the error from the bean method to the container (i.e., the bean method does not have to catch the exception).
But I don't understand it. Does it mean that I shouldn't catch all types of exceptions (i.e. try to catch
Exception
class) and rethrow it as my application exception?An example for more clarity:
public void beanMethod throws MyApplicationException { try { // do something } catch (Exception e) { throw new MyApplicationException(e); // Should I do it like this? } }
Or is this not for EJB developers, but only for EJB reference-implementation developers (container developers): In the latter case, as a consequence, the container must not propagate system exceptions to my business method, and my
catch(Exception e)
block never catches any system exception?
原文:https://stackoverflow.com/questions/14408369
最满意答案
这是什么其他用例,
简洁(因此,只要你习惯了它,因为毕竟它根本不会牺牲可读性!) - 任何时候你需要检查一些东西,或者如果它是真的,就使用它,或者是其他值是错误的(这是为了 - 为 -
or
反转它 - 而且我非常故意避免实际的关键字 - 或者像True
和False
,因为我在谈论每个对象,而不仅仅是bool
!)。任何计算机屏幕上的垂直空间都是有限的,并且在给出选择的情况下,最好花费在有用的可读性辅助工具上(文档集,评论,策略性地将空行分开块)
inverses = [x and 1.0/x for x in values]
成六个如:
inverses = [] for x in values: if x: inverses.append(1.0/x) else: inverses.append(x)
或更狭窄的版本。
和/或这个相当不直观的实施的基本原理是什么?
由于一些语言(如标准Pascal)没有详细说明评估顺序和
and
和or
的短路性质,所以初学者通常不会被“不直观”所绊住。 Turbo Pascal和语言标准之间的差异之一是Turbo当时最流行的Pascal方言,它正是Turbo实现的and
并且很像Python以后(以及C语言早期的版本.. )。What are other use cases of this,
Conciseness (and therefore clarity, as soon as you get used to it, since after all it does not sacrifice readability at all!-) any time you need to check something and either use that something if it's true, or another value if that something is false (that's for
and
-- reverse it foror
-- and I'm very deliberately avoiding the actual keywords-or-the-likeTrue
andFalse
, since I'm talking about every object, not justbool
!-).Vertical space on any computer screen is limited, and, given the choice, it's best spent on useful readability aids (docstrings, comments, strategically placed empty lines to separate blocks, ...) than in turning, say, a line such as:
inverses = [x and 1.0/x for x in values]
into six such as:
inverses = [] for x in values: if x: inverses.append(1.0/x) else: inverses.append(x)
or more cramped versions thereof.
and/or what is the rationale for this rather unintuitive implementation?
Far from being "unintuitive", beginners regularly were tripped up by the fact that some languages (like standard Pascal) did not specify the order of evaluation and the short-circuiting nature of
and
andor
; one of the differences between Turbo Pascal and the language standard, which back in the day made Turbo the most popular Pascal dialect of all times, was exactly that Turbo implementedand
andor
much like Python did later (and the C language did earlier...).
相关问答
更多-
这是什么其他用例, 简洁(因此,只要你习惯了它,因为毕竟它根本不会牺牲可读性!) - 任何时候你需要检查一些东西,或者如果它是真的,就使用它,或者是其他值是错误的(这是为了 - 为 - or反转它 - 而且我非常故意避免实际的关键字 - 或者像True和False ,因为我在谈论每个对象,而不仅仅是bool !)。 任何计算机屏幕上的垂直空间都是有限的,并且在给出选择的情况下,最好花费在有用的可读性辅助工具上(文档集,评论,策略性地将空行分开块) inverses = [x and 1.0/x for x ...
-
正如其他答案所提到的那样,你可以重载运算符(通过在你写的类中定义一些特殊的方法,即名称以两个下划线开头和结尾的方法)。 所有的细节都在这里 。 要完成您的答案问题:您不能定义新的运算符; 但是<<不是一个新的运算符,它是一个现有的运算符,并且通过在类中定义方法__lshift__ 。 作为一个历史记录,这也是C ++中的情况 - 但是您可以重载的确切的运算符集在两种语言之间是不同的。 例如,在C ++中,不能重载属性访问. ; 在Python中,可以使用__getattr__ (或__getattribut ...
-
这不是因为没有意义 将“x ++”定义为“x + = 1,评估x的先前绑定”是非常有意义的。 如果你想知道原来的原因,你将不得不通过旧的Python邮件列表或者询问那些在那里的人(例如Guido),但是事实证明是很容易的: 与其他语言一样,不需要简单的递增和递减。 你不要经常在Python中编写for(int i = 0; i < 10; ++i) ; 相反,你做的事情像for i in range(0, 10) 。 由于不需要像往常一样,所以有更少的理由给它自己的特殊语法; 当你需要增加时, +=通常很好 ...
-
例如, 可以将函数的文本表示存储在变量中并对其进行评估 test = 'x > 0'; eval(test) 应根据x的值得出1或0。 但是你也不应该因为理由而使用eval - 这里常常会涉及到,所以我不得不重复。 您应该熟悉功能和功能句柄。 例如 test = @(x)x>0 使test成为测试其参数是否大于0的函数的句柄。 与编译语言相反,在运行时解释的许多语言具有相似的功能。 You can store the textual representation of a function in a ...
-
Python中的新操作符(New operators in Python)[2022-03-17]
正如@minitech所说的,你不能定义新的运营商。 但检查这个黑客,可以让你定义中缀操作符http://code.activestate.com/recipes/384122-infix-operators/ Expanding on @fasouto answer, but adding a bit more code. While you cannot define new operators AND you cannot redefine existing operators for built-i ... -
Python中的按位运算(Bitwise operations in Python)[2023-10-24]
在执行任何操作之前,您始终可以添加& ((1<<32) - 1)掩码以将数量限制为32位,例如 class Int32(int): def __neg__(self): return Int32(int.__neg__(self) & ((1 << 32) - 1)) def __rshift__(self, other): if self & (-1 << 31): retval = int.__rshift__(int.__sub ... -
如果您想在标准库中使用按位运算符的具体示例,请查看re库。 根据API ,标志应该按位OR一起进行。 这允许您在单个参数中传递大量选项。 请考虑以下选项: re.compile(expression,re.I | re.M | re.X) 与 re.compile(expression,ignorecase=True,multiline=True,verbose=True) 我想我们可以同意第一个版本至少更紧凑。 你可能会想“嗯,我更喜欢第二个 - 毕竟,它更明确!” ......你可能有一个案例。 但 ...
-
是的,第二个代码块等同于第一个代码块。 根据文档 , or比and优先级低。 这意味着if语句被评估为 if ((hand[0] in winning_cards and hand[1] == 'Ace') or (hand[0] == 'Ace' and hand[1] in winning_cards)): 这是你想要的。 您可以返回该布尔表达式的结果来缩短代码: def blackjack_check(hand): winning_cards = [10, 'Jack', 'Que ...
-
你最好不要将操作符指定为文本 。 只需使用lambda表达式或operator模块: import operator import random operators = [operator.add,operator.sub,operator.mul,operator.floordiv] op = random.choice(operators) 然后你可以用两个参数来调用它,比如: result = op(2,3) 例如: >>> import operator >>> import random >>> ...
-
不,对不起。 尽管你可以编写一个接口(可能作为装饰器或基类),使得这很容易,如果你需要不止一次地做。 或者使用已经提供的一个。 (猜猜我已经40多岁了。) No, sorry. Though you could write an interface (probably as a decorator or base class) that makes this easy, if you need to do it more than once. Or use one provided already. (Gu ...