捕获异常而不进入其 try 块?
我有一个有趣的问题。我的代码块如下所示。问题是:日志文件不包含“准备睡觉......”行,但包含“备份线程被中断......”行。 我的问题:是否可以在不进入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,不可能捕获在执行
try
块期间未抛出的异常。可能的情况:InterruptedException
在您记录“准备就绪...”
之前抛出,即在您的log(...)
调用中。 (不太可能)Log 方法无法按预期工作,并且不会记录您的行。 (可能)您可以检查是否通过设置较长的睡眠时间来执行睡眠。这暗示了这个原因。
您还有其他代码记录
“备份线程被中断...”
导致日志输出,并且所提供的代码片段根本不执行。No it is not possible to catch an Exception not thrown during execution of its
try
block. Possible situations:InterruptedException
is thrown before you log"Getting ready..."
i.e. in yourlog(...)
call. (unlikely)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.
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.我的猜测是日志方法无法正常工作?我不相信不进入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?
如果您进入
catch
块,则try
块内会引发异常。时期。要解决您的问题:打印异常的堆栈跟踪并查看
log()
是否也引发一些异常。If you enter the
catch
block an exception has been thrown inside thetry
block. Period.To solve your problem: Print the stacktrace of the exception and look if
log()
also throws some exceptions.