调用Exception.printStackTrace时发生AbstractMethodError [英] AbstractMethodError on calling Exception.printStackTrace

查看:113
本文介绍了调用Exception.printStackTrace时发生AbstractMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在catch子句中,我想打印该异常的痕迹:

Inside a catch clause I want to print the strack trace of the exception:

try {
    ...
} catch (Exception exc) {
    exc.printStackTrace();
    ...
}

但在某些情况下,我听不到堆栈跟踪,而是看到类似这样的东西:

But in some cases I don't get a stack trace and instead see something like this:

Exception in thread "pool-1-thread-2" java.lang.AbstractMethodError: java.lang.Exception.printStackTrace()V
    ...

通常,如果库在运行时的版本与编译时的版本不同,则应该发生此异常,但是在这种情况下,我使用Java库中的类。 printStackTrace在Throwable中实现,因此该方法不能在Exception或任何派生类中抽象。此外,此AbstractMethodError并不总是抛出,有时在此特定的catch子句中还有其他异常(程序流取决于文件和当前时间的数据,因此有时还会发生其他事情,例如在我自己的代码中抛出的ArrayIndexOutOfBoundsExceptions或IllegalStateExceptions

Usually, this exception should occur if a library has not the same version at runtime as at compile time, but in this case I work with a class from the Java library. printStackTrace is implemented in Throwable, so this method can't be abstract in Exception or any derived class. Further, this AbstractMethodError isn't always thrown, sometimes there are other exceptions at this specific catch clause (program flow depends on data from a file and the current time, so sometimes other stuff happens like ArrayIndexOutOfBoundsExceptions or IllegalStateExceptions that are thrown in my own code and that I would expect instead of the strange error).

所以,问题是:特定的AbstractMethodError怎么可能发生?

So, the question is: How is it possible for that particular AbstractMethodError to occur?

PS:我在Linux上使用Eclipse Helios,并使用JDK 1.6.0_24作为运行时环境来启动我的应用程序。

PS: I am using Eclipse Helios on Linux and use JDK 1.6.0_24 as Runtime Environment to launch my application.

编辑:,有一个错字(printStrackTrace),我已更正。只是被我遗忘了,与我的问题无关。它是(或应该是)一个独立的应用程序,一个Web应用程序,一个Eclipse RCP应用程序,一个普通的旧Java应用程序(或多或少)。这个问题的确在另一台计算机上也发生过-Eclipse Helios,Fedora Linux和JDK 1.6.0_21也是如此。

There was a typo (printStrackTrace), I corrected it. Was just written out of my mind and hasn't anything to do with my problem. It is (or should be) a standalone application, no web-application, no Eclipse RCP application, just a plain old Java application (more or less). The problem does occur on another computer, too - also with Eclipse Helios, also with Fedora Linux, but with JDK 1.6.0_21.

令我惊讶的是,确实有可能调用 getClass()。getName()(但没有尝试其他方法),该异常的类型为 java.lang.ArrayIndexOutOfBoundsException 。我只是尝试使用OpenJDK 1.6.0(因为它已经安装在我的系统上)并得到了不同的结果。 printStackTrace 不会抛出 AbstractMethodError ,而只是打印出空行和 getMessage()返回 null (而不是引发错误)。因此,我不知道该异常的确切位置,因为层次结构中较高的try-catch-block捕获该异常只是为了优雅地停止应用程序的一部分。我可能会在某些方面发现这种异常类型,以了解异常的来源。但这并不能解释异常本身的奇怪行为。

To my surprise it was indeed possible to call getClass().getName() (but no other method I tried), the exception is of type java.lang.ArrayIndexOutOfBoundsException. I just tried to use OpenJDK 1.6.0 (because it is already installed on my system) and got different results. Instead of throwing the AbstractMethodError, printStackTrace printed just an empty line and getMessage() returned null (instead of throwing the error). So I don't know where exactly the exception was thrown, because a try-catch-block high up in hierarchy catches the exception just to stop a part of the application gracefully. I might catch this exception type on some points to get an idea where it comes from. But that won't explain the strange behavior of the exception itself.

编辑2:最后,我找到了问题所在。事实证明,这是昨天在我的同一行已经发生的异常,但有时异常本身的行为很奇怪。我在 List 上调用 get(int)(更确切地说: ArrayList 使用 Collections.unmodifiableList(List))包装,索引为 -1 是初始值,应在循环内更改此索引,但无论出于何种原因都不会更改)。至少现在我知道在哪里可以解决 ArrayIndexOutOfBoundsException 的问题,但我仍然不知道异常本身的行为为何奇怪。

Edit 2: Finally I tracked the problem down. It turned out to be an exception that occurred to me already yesterday at the exact same line, but sometimes the exception itself behaves strange. I call get(int) on a List (more precisely: an ArrayList that was wrapped using Collections.unmodifiableList(List)) with an index that is -1 (that is the initial value, inside a loop this index should be changed, but it will not for whatever reason). At least now I know where to look to fix the ArrayIndexOutOfBoundsException, but I still have no clue why the exception itself behaves strange.

编辑3:我尝试了 Throwable.class.getMethod( printStackTrace)。invoke(exc); 而不是 exc.printStackTrace(); 并得到 java.lang.NoSuchMethodError:java.lang.Throwable.printStackTrace()V java.lang.AbstractMethodError 。我还尝试使用 javac jar (只有一个库例外)从外壳编译Java文件。 ,因为手动编译所有jar会很麻烦。结果是一样的。如果我自己在该行上抛出 IndexArrayOutOfBoundsException ,则堆栈跟踪将被很好地打印。也许我不得不希望这个问题非常罕见,并且永远不会在其他任何地方发生。

Edit 3: I tried Throwable.class.getMethod("printStackTrace").invoke(exc); instead of exc.printStackTrace(); and got a java.lang.NoSuchMethodError: java.lang.Throwable.printStackTrace()V instead of the java.lang.AbstractMethodError. I also tried to compile the java files from the shell using javac and jar (only the one library the exception is coming from, because it would be tedious to manually compile all jars). The result is the same. If I throw an IndexArrayOutOfBoundsException myself at that line, the stack trace will be printed just fine. Maybe I have to hope that this problem is very rare and will never occur anywhere else.

推荐答案

据我了解,您已经启动了Eclipse Helios中的应用程序。这可能是由于Eclipse与OSGi和专用的classloader的模块化所致:用于运行您的应用程序的ClassLoader层次结构可能导致无效的链接。它主要取决于关注的异常类以及应用程序类路径中包含的jar文件-您自己的jar之一可能与Eclipse本身使用的jar文件冲突。您可以提供其他详细信息吗?

As I understand you have launched your application inside Eclipse Helios. This may be the reason because of the modularity of Eclipse with OSGi and dedicated classloader: the ClassLoader hierarchy used to run your application may lead to invalid linkage. It mainly depends on what exception class is concerned and which jar files you have included in your application classpath - one of your own jar may conflict with a jar file used in Eclipse itself. May you provide additional details ?

我想您是使用 java应用程序运行配置启动它的。您是否选中了包括系统库或包括继承的主库之类的复选框或任何其他选项?

I guess you launch it with a "java application" run configuration. Have you checked one of the checkboxes like "Include system libraries" or "Included inherited main" or any other options ?

顺便说一句,您的应用程序最终将以

By the way, your application will finally run as a standalone Java virtual machine and it should run perfectly well in that context.

如果不是这种情况,请使用 -verbose运行Java应用程序: class 命令行选项,并在崩溃前检查输出中的最新行,您可能会了解到冲突的库(运行时与编译时有所不同)。

If it is not the case, run your java application with -verbose:class command-line option and inspect latest line in output before the crash, you may get a clue about the conflictual libraries (different at runtime compared to compile-time).

这篇关于调用Exception.printStackTrace时发生AbstractMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆