捕获异常而不进入其 try 块?

发布于 2024-12-09 13:31:55 字数 1141 浏览 2 评论 0原文

我有一个有趣的问题。我的代码块如下所示。问题是:日志文件不包含“准备睡觉......”行,但包含“备份线程被中断......”行。 我的问题:是否可以在不进入 try 块的情况下捕获异常?

long sleepTime = runtime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();

try {
    log("Getting ready to sleep...(" + sleepTime + ")");

    Thread.sleep(sleepTime);

    Database db = new Database();
    log(db.performDatabaseBackup());

    // Set it to run the next day.
    runtime.add(Calendar.DATE, 1);

  } catch (InterruptedException ie) {
    log("Backup thread was interrupted...");
}

编辑:记录方法

  private void log(String message) {
                   Logger.debug(this, message);
}

和 Logger.debug

public static void debug(
      Object obj,
      String message ){
      debug( obj.getClass(), message );
      }


    public static void debug(
      String className,
      String message )
   {
      try
      {
         Class inputClass = Class.forName( className );
         debug( inputClass, message );
      }
      catch ( ClassNotFoundException e )
      {
         debug( ( Class )null, message );
      }
   }

I have an interesting issue. My code block is given below. The problem is : log file does not contains "Getting ready to sleep.." line but it contains "Backup thread was interrupted..." lines.
My question : Is it possible to catch exception without getting in its try block ?

long sleepTime = runtime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();

try {
    log("Getting ready to sleep...(" + sleepTime + ")");

    Thread.sleep(sleepTime);

    Database db = new Database();
    log(db.performDatabaseBackup());

    // Set it to run the next day.
    runtime.add(Calendar.DATE, 1);

  } catch (InterruptedException ie) {
    log("Backup thread was interrupted...");
}

Edit : Log method

  private void log(String message) {
                   Logger.debug(this, message);
}

and Logger.debug

public static void debug(
      Object obj,
      String message ){
      debug( obj.getClass(), message );
      }


    public static void debug(
      String className,
      String message )
   {
      try
      {
         Class inputClass = Class.forName( className );
         debug( inputClass, message );
      }
      catch ( ClassNotFoundException e )
      {
         debug( ( Class )null, message );
      }
   }

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

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

发布评论

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

评论(3

沉溺在你眼里的海 2024-12-16 13:31:55

不,不可能捕获在执行 try 块期间未抛出的异常。可能的情况:

  1. InterruptedException 在您记录 “准备就绪...” 之前抛出,即在您的 log(...) 调用中。 (不太可能)

  2. Log 方法无法按预期工作,并且不会记录您的行。 (可能)您可以检查是否通过设置较长的睡眠时间来执行睡眠。这暗示了这个原因。

  3. 您还有其他代码记录 “备份线程被中断...” 导致日志输出,并且所提供的代码片段根本不执行。

No it is not possible to catch an Exception not thrown during execution of its try block. Possible situations:

  1. InterruptedException is thrown before you log "Getting ready..." i.e. in your log(...) call. (unlikely)

  2. Log method does not work as expected and does not log your line. (likely) Can you check if the sleep is performed by setting a high sleep time. That would imply this reason.

  3. You have other pieces of code logging "Backup thread was interrupted..." that cause the log output and the presented code fragement is not executed at all.

变身佩奇 2024-12-16 13:31:55

我的猜测是日志方法无法正常工作?我不相信不进入try块就可以进入catch块。另一种选择是 log 方法抛出 InterruptedException?

您也可以发布您的日志方法吗?

My guess would be that the log method is not functioning properly? I don't believe that the catch block can be entered without entering the try block. Another alternative is that the log method throws InterruptedException?

Can you post your log method as well?

痴梦一场 2024-12-16 13:31:55

如果您进入 catch 块,则 try 块内会引发异常。时期。

要解决您的问题:打印异常的堆栈跟踪并查看 log() 是否也引发一些异常。

If you enter the catchblock an exception has been thrown inside the try block. Period.

To solve your problem: Print the stacktrace of the exception and look if log() also throws some exceptions.

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