加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 记录或重新抛出此异常

发布时间:2020-12-15 04:29:13 所属栏目:Java 来源:网络整理
导读:可能重复 Sonar complaining about logging and rethrowing the exception. 这是我在课堂上的代码: try { this.processDeepLinkData(data); } catch (final Exception e) { // Error while parsing data // Nothing we can do Logger.error(TAG,"Exception
可能重复 Sonar complaining about logging and rethrowing the exception.

这是我在课堂上的代码:

try
    {
        this.processDeepLinkData(data);
    }
    catch (final Exception e)
    {
        // Error while parsing data
        // Nothing we can do
        Logger.error(TAG,"Exception thrown on processDeepLinkData. Msg: " + e.getMessage());
    }

和我的Logger类:

import android.content.Context;
    import android.util.Log;
    import com.crashlytics.android.Crashlytics;

    public final class Logger
    {
        /**
         * Convenience method.
         *
         * @see Logger#log(String,String)
         */
        public static void error(final String tag,final String msg)
        {
            if (Logger.DEBUG)
            {
                Log.e(tag,"" + msg);
            }
            else
            {
                Logger.log(tag,"" + msg);
            }
        }

        private static void log(final String tag,final String msg)
        {
            Crashlytics.log(tag + ": " + msg);
        }
}

声纳指向捕获(最终异常e)并说要么记录或重新抛出此异常.你怎么看?

解决方法

如果查看规则说明:
https://rules.sonarsource.com/java/RSPEC-1166

尤其是标题:

Exception handlers should preserve the original exception

在您的情况下,您只关注异常的消息,因此不保留最终的堆栈跟踪(以及失败的根本原因).

此规则检测到您没有将捕获的异常用作catch块中的整个对象.

这可能不适合您的情况:将规则标记为“不会修复”或在质量配置文件中将其停用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读