首页 \ 问答 \ EJB 3.0异常处理(EJB 3.0 exceptions handling)

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
更新时间:2022-01-25 21:01

最满意答案

这是什么其他用例,

简洁(因此,只要你习惯了它,因为毕竟它根本不会牺牲可读性!) - 任何时候你需要检查一些东西,或者如果它是真的,就使用它,或者是其他值是错误的(这是为了 - 为 - or反转它 - 而且我非常故意避免实际的关键字 - 或者像TrueFalse ,因为我在谈论每个对象,而不仅仅是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)没有详细说明评估顺序和andor的短路性质,所以初学者通常不会被“不直观”所绊住。 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 for or -- and I'm very deliberately avoiding the actual keywords-or-the-like True and False, since I'm talking about every object, not just bool!-).

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 and or; 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 implemented and and or 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 ...
  • 正如@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 ...
  • 在执行任何操作之前,您始终可以添加& ((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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)