关于 java Throwable
当我开发android应用程序时,我想制作一个CrashReport类,然后使用它向我的服务器发送报告。
我制作了一个名为 CrashHandler 的类,它实现了 UncaughtExceptionHandler,并故意制作了一个在 Activity 中导致 NullPoint 错误的示例,该类运行良好,但有一点问题... 在方法 uncaughtException 中:
@Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace(); //**code line 1**
....//some other codes and variables
StackTraceElement[] elements = ex.getStackTrace();
for (StackTraceElement element : elements) { // **code line 2**
errorString = errorString + "<br/>" + element.toString();
}
}
代码行 1 将堆栈跟踪打印为:
java.lang.NullPointerException
my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
但代码行仅显示:
my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
您注意到它没有显示:
"java.lang.NullPointerException"
我知道我没有调用 getCause() 但是当尝试使用 getCause() 时,它返回 null,而不是 throwable;
我试图查看 throwable 类的源代码...但注意到
你能告诉我为什么 getCause 为 null 但 printStackTrace 有一个错误原因打印出来吗?
when I develop android application,I want to make a CrashReport class and then use it send report to my server.
I make a class called CrashHandler which implement UncaughtExceptionHandler,and make a example to cause a NullPoint error in Activity in purpose,the class works well , but a little problem...
in method uncaughtException:
@Override
public void uncaughtException(Thread thread, Throwable ex) {
ex.printStackTrace(); //**code line 1**
....//some other codes and variables
StackTraceElement[] elements = ex.getStackTrace();
for (StackTraceElement element : elements) { // **code line 2**
errorString = errorString + "<br/>" + element.toString();
}
}
code line 1 print the stack trace as:
java.lang.NullPointerException
my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
but the codeline only show:
my.app.ExceptionCaughtActivity$1.onClick(ExceptionCaughtActivity.java:25)
android.view.View.performClick(View.java:2486)
android.view.View$PerformClick.run(View.java:9130)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:130)
android.app.ActivityThread.main(ActivityThread.java:3703)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
dalvik.system.NativeStart.main(Native Method)
you notice it not showed the:
"java.lang.NullPointerException"
I konw I do not called the getCause()
but when try to use getCause(), it return a null, not a throwable;
I tried to view the source code of throwable class...but got noting
can you tell me why the getCause is null but printStackTrace have a error cause to print out ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ex.printStackTrace()
还可以帮助打印 Throwable 对象ex
的类和消息(如果有)作为跟踪信息。请参阅 Throwable.toString()。原始堆栈跟踪本身不包含此信息,但您可以从 Throwable 对象获取它,该对象可能有也可能没有消息,并且可能有也可能没有原因。但它总是有一个类! ;-)
这是来自
Throwable
的源代码:ex.printStackTrace()
helpfully prints the class and message (if any) of the Throwable objectex
as well as the trace information. SeeThrowable.toString()
.The raw stack trace itself doesn't include this information but you can get it from the Throwable object, which may or may not have an message, and may or may not have a cause. It always has a class though! ;-)
Here's the source code from
Throwable
:如果
可抛出
位于异常链的顶部,则getCause()
返回null
。它是原因,所以它不可能有原因。您可以在此处阅读有关异常链接的信息。
getCause()
returnsnull
if thethrowable
is at the top of the exception chain. It is the cause so it can't have a cause.You can read about exception chaining here.
您尝试过 getMesssage 或 getLocalizedMessage 吗?
http://docs.oracle.com/javase /6/docs/api/java/lang/Throwable.html#getMessage()
Have you tried getMesssage or getLocalizedMessage??
http://docs.oracle.com/javase/6/docs/api/java/lang/Throwable.html#getMessage()
第一行是从这里得到的:
所以如果你想添加第一行使用:
First line is getting from this:
So if you want to add first line use: