log4j异常处理问题

发布于 2024-09-29 14:11:56 字数 419 浏览 0 评论 0原文

我有一个初始化 log4j 的类。这段代码永远不会打印或退出,我不明白为什么。

public class MyLog
{

   private static Logger log;

   static
   {
      log = Logger.getRootLogger();
      try
      {
            PropertyConfigurator.configure("somefileNameWhichDoesNotExist");
      }
      catch(Exception t)
      {
            System.out.println("oops logging cant be set, lets exit");
            System.exit(0);
      }

I have a class which initializes my log4j. This code will never print or exit, I dont understand why.

public class MyLog
{

   private static Logger log;

   static
   {
      log = Logger.getRootLogger();
      try
      {
            PropertyConfigurator.configure("somefileNameWhichDoesNotExist");
      }
      catch(Exception t)
      {
            System.out.println("oops logging cant be set, lets exit");
            System.exit(0);
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乙白 2024-10-06 14:11:56

为什么假设文件不存在时会抛出异常?我刚刚快速浏览了 API 文档,他们没有提及任何有关丢失文件的处理 - 因此该条件很可能会被忽略。


编辑:只需阅读您的附加评论,所以情况并非如此。

确保静态块实际上正在执行。


编辑:PropertyConfigurator 正在捕获异常,并在内部处理它。这就是为什么你看不到异常。 请参阅源代码 - 第 370-380 行

Why do you assume that an exception will be thrown when the file doesn't exist? I just had a quick look at the API docs, and they don't say anything about the handling of missing files - so it's just as likely that the condition would just be ignored.


EDIT: just read your additional comments, so that's not the case.

Make sure that the static block is actually executing.


EDIT: PropertyConfigurator is catching the exception, and handling it internally. That's why you don't see the exception. See the source - lines 370-380.

南街九尾狐 2024-10-06 14:11:56

PropertyConfigurator#configure (String configFilename) 不会抛出任何已检查的 Exception,因此 catch(Exception t) 永远不会捕获任何内容。检查 ApiDoc,因为当你想捕获异常时,在 throws 子句中必须有一个声明的异常。

PropertyConfigurator#configure(String configFilename) does not throw any checked Exception, therefore there is never anything to catch with catch(Exception t). Check the ApiDoc, because there has to be a declared Exception in the throws-clause, when you want to catch Exception.

迟月 2024-10-06 14:11:56

你能告诉我,找到问题的答案了吗?
这对我来说非常有用。

我的解决方案是:

try
{
    Properties props = new Properties();
    props.load(new FileInputStream(propFilePath));
    PropertyConfigurator.configure(props);
}
catch (FileNotFoundException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
}

也许你有更好的解决方案?
多谢!

Could you tell me, have found the answer to the question?
It will be very useful for me.

My solution is:

try
{
    Properties props = new Properties();
    props.load(new FileInputStream(propFilePath));
    PropertyConfigurator.configure(props);
}
catch (FileNotFoundException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
}

May be you have a better one?
Thanks a lot!

浅暮の光 2024-10-06 14:11:56

难道是configure方法抛出了Error?

更改代码以捕获 java.lang.Throwable (http://download.oracle.com/javase/6/docs/api/java/lang/Throwable.html) 而不是 Exception。

Could it be that the configure method is throwing an Error?

Change your code to catch a java.lang.Throwable (http://download.oracle.com/javase/6/docs/api/java/lang/Throwable.html) instead of Exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文