English 中文(简体)
哪一类可浏览量,哪些次应该排入?
原标题:Which subclass of Throwable should be caught and which shouldn t?

标语: 标语 这是否意味着Error和例外之间的区别在于告诉方案人员,应当抓住哪一个子级,哪些是应该的? 还是有更多的东西?

最佳回答

一般而言,<代码>Error是严重错误的(通常,平台本身),你无法想象。 我一直关注到的只有<代码>Error,才能做到log it,此后我再行。

这一点至关重要,因为容易让错误(和暂时的例外情况)传播呼吁,从而永远不会被打脚(例如,使用<条码>executorservice.submit(Runnable),而不必听回<条码>Future)。

www.un.org/Depts/DGACM/index_russian.htm

  • out of memory
  • abstract method error (e.g. running against different version of libraries to those built against)
  • Assertions (i.e. programmer-defined invariants, or things that should never happen - lol!)

然后,我要说,<代码>RuntimeException通常(但并不总是)表示programming<>/em>错误:

  • not checking for null, or passing in null
  • passing in invalid arguments, or allowing invalid state
  • modifying a collection as you are iterating over it

我通常也建议不要这样做,但这是一个灰色地区;或许你在将用户投入转交服务器之前不核对用户的意见,而这样做是很不值得的。

检查的<代码>Exceptions(即非营业时间)应当用于你可以合理预计会发生并合理(或可预见)处理你的代码。 我个人喜欢检查例外情况,但由于以同样方式处理不同例外类型(即多块相同的捕获区)所涉及的两岸/重犯,这些例外情况变得十分麻烦。 象Schala这样的语言更能捕获合成物,但随后它们也删除了checked<>-em>的例外情况。

问题回答

是的,我认为你的分析在这里是正确的——你不应捕获<代码>。 Error,因为它们是无法从等系统中收回的错误。 OutOfMemoryError

捕获<代码>可浏览<>的<>代码>的唯一原因是,如果你掌握外部第三方代码,而这是正确操作你的方案所不需要的——如果你不相信守则、收集一切材料,如果你 st(<代码>可浏览<<>>>>>,那么该代码就会失控并报告。

另外,区分<代码>Exception <>/code>和RuntimeException可能是一个好的想法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...