关于 java Throwable

发布于 2025-01-04 04:29:40 字数 2334 浏览 1 评论 0原文

当我开发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 技术交流群。

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

发布评论

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

评论(4

酒废 2025-01-11 04:29:40

ex.printStackTrace() 还可以帮助打印 Throwable 对象 ex消息(如果有)作为跟踪信息。请参阅 Throwable.toString()。

原始堆栈跟踪本身不包含此信息,但您可以从 Throwable 对象获取它,该对象可能有也可能没有消息,并且可能有也可能没有原因。但它总是有一个类! ;-)

这是来自 Throwable 的源代码:

public void printStackTrace(PrintStream s) {
    synchronized (s) {
        s.println(this);
        StackTraceElement[] trace = getOurStackTrace();
        for (int i=0; i < trace.length; i++)
            s.println("\tat " + trace[i]);

        Throwable ourCause = getCause();
        if (ourCause != null)
            ourCause.printStackTraceAsCause(s, trace);
    }
}

ex.printStackTrace() helpfully prints the class and message (if any) of the Throwable object ex as well as the trace information. See Throwable.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:

public void printStackTrace(PrintStream s) {
    synchronized (s) {
        s.println(this);
        StackTraceElement[] trace = getOurStackTrace();
        for (int i=0; i < trace.length; i++)
            s.println("\tat " + trace[i]);

        Throwable ourCause = getCause();
        if (ourCause != null)
            ourCause.printStackTraceAsCause(s, trace);
    }
}
乖乖公主 2025-01-11 04:29:40

如果可抛出位于异常链的顶部,则getCause()返回null。它是原因,所以它不可能有原因。

您可以在此处阅读有关异常链接的信息。

getCause() returns null if the throwable 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.

溇涏 2025-01-11 04:29:40

您尝试过 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()

霓裳挽歌倾城醉 2025-01-11 04:29:40

第一行是从这里得到的:

public String toString() {
    String s = getClass().getName();
    String message = getLocalizedMessage();
    return (message != null) ? (s + ": " + message) : s;
}

所以如果你想添加第一行使用:

errorString += ex.toString + "<br/>";
StackTraceElement[] elements = ex.getStackTrace(); 
for (StackTraceElement element : elements) {       // **code line 2**
    errorString = errorString + "<br/>" + element.toString();
}

First line is getting from this:

public String toString() {
    String s = getClass().getName();
    String message = getLocalizedMessage();
    return (message != null) ? (s + ": " + message) : s;
}

So if you want to add first line use:

errorString += ex.toString + "<br/>";
StackTraceElement[] elements = ex.getStackTrace(); 
for (StackTraceElement element : elements) {       // **code line 2**
    errorString = errorString + "<br/>" + element.toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文