English 中文(简体)
习俗例外。 标识
原标题:Custom exception. Where to log it

I created a CustomExceptionClass that its messages are from a configuration file and are a friendlier message to the user.

Then I want to log wich exception was thrown because, if something went wrong, I want to know details then I can fix this. So, I have a doubt.

Where to log these exceptions using log4j? Inside the CustomExceptionClass, or I let the method that throws this exception log it?

最佳回答

You should log wherever you are catching the Exception. If you are not catching it anywhere it really depends how you are running your application.

问题回答

我不敢肯定,你是否特别对原木4j有问题,但APIC只是需要一些东西来打log.eror(Object,Throwable),将电文作为第一个参数传递,并将错误作为第二个参数。

Regarding the question of where to call log.error, do not call log.error from within your CustomExceptionClass subclass of Throwable. Instead, I d make the decision as follows:

If you want to log details about exactly what happened, but you don t plan to put those details into your subclass of Exception, then log the details before throwing the error.

同样,如果你想要做一些具体的事情,无论是否或如何抓住例外,那么显然在放弃例外之前需要这样做。 你对谁使用非私人方法的控制相对较少。

否则,在捕获区就会出现记录。 这使你能够跟踪所发生情况以及你的申请如何反应。 如果你只掌握了第一件资料,那么其他人就必须阅读该守则,以了解“什么?”

最后,它认为,在一切透镜中使用非专业例外处理器是一种良好做法。 详情见Thread. UncaultExceptionHandler。 基本上,你确实希望至少记录长期申请的所有例外。

您应将其登录在错误处理代码中,而不是最初产生错误之处。

在习俗例外情况下,我通常超负荷工作,只能正常工作。





相关问题
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 ...

热门标签