在Java或C#异常管理的最佳实践 [英] Best practices for exception management in Java or C#

查看:196
本文介绍了在Java或C#异常管理的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卡决定如何处理我的应用程序异常。

I'm stuck deciding how to handle exceptions in my application.

多,如果我有例外问题来自1)通过远程服务访问数据或2)反序列化的JSON对象。不幸的是,我不能保证任一任务成功(切的网络连接,畸形的JSON对象,它是在我的掌握)。

Much if my issues with exceptions comes from 1) accessing data via a remote service or 2) deserializing a JSON object. Unfortunately I can't guarantee success for either of these tasks (cut network connection, malformed JSON object that is out of my control).

这样一来,如果我做遇到异常,我只是在函数内抓住它,并返回FALSE给调用者。我的逻辑是,所有的来电者真正关心的是,如果任务成功,不是它为什么没有成功。

As a result, if I do encounter an exception I simply catch it within the function and return FALSE to the caller. My logic is that all the caller really cares about is if the task was successful, not why it is wasn't successful.

下面是一个典型的方法的一些示例code(在JAVA))

Here's some sample code (in JAVA) of a typical method)

public boolean doSomething(Object p_somthingToDoOn)
{
    boolean result = false;

    try{
        // if dirty object then clean
        doactualStuffOnObject(p_jsonObject);

        //assume success (no exception thrown)
        result = true;
    }
    catch(Exception Ex)
    {
        //don't care about exceptions
        Ex.printStackTrace();
    }
    return result;
}

我觉得这个方法是好的,但我真的很好奇,想知道什么是最好的做法是管理异常(应我真的泡一个例外一路上涨调用堆栈?)。

I think this approach is fine, but I'm really curious to know what the best practices are for managing exceptions (should I really bubble an exception all the way up a call stack?).

在关键问题总结:


  1. 是不是还好只是捕捉异常,但不是泡沫起来或正式通知系统(通过日志或向用户通知)?

  2. 哪些最佳实践在那里为例外不会导致一切需要try / catch块?

跟进/修改

感谢所有的反馈,发现异常管理网上一些优秀的来源:

Thanks for all the feedback, found some excellent sources on exception management online:

  • Best Practices for Exception Handling | O'Reilly Media
  • Exception Handling Best Practices in .NET
  • Best Practices: Exception Management (Article now points to archive.org copy)
  • Exception-Handling Antipatterns

似乎异常管理是有所不同的基于上下文的事情之一。但最重要的,应该是如何管理一个系统内的异常一致。

It seems that exception management is one of those things that vary based on context. But most importantly, one should be consistent in how they manage exceptions within a system.

此外通过过度的尝试/捕获留意code腐与否给人一种异常的尊重(异常警告系统,需要予以警告什么?)。

Additionally watch out for code-rot via excessive try/catches or not giving a exception its respect (an exception is warning the system, what else needs to be warned?).

此外,这是一个pretty选择从 m3rLinEz

Also, this is a pretty choice comment from m3rLinEz.

我倾向于安德斯·海尔斯伯格同意,你只有最呼叫者
  关心,如果操作成功与否。

I tend to agree with Anders Hejlsberg and you that the most callers only care if operation is successful or not.

从这个评论它带来了一些问题,想处理异常时:

From this comment it brings up some questions to think about when dealing with exceptions:


  • 什么是抛出该异常的点?

  • 如何是否有意义处理呢?

  • 是否来电真正关心的异常或做他们只关心调用成功?

  • 迫使呼叫者来管理潜在的异常优美?

  • 您是尊重语言的idoms?

    • 请你真的需要返回一个成功标志像布尔?返回布尔(或int),更是一个C心态不是一个Java(在Java中,你将只处理异常)之一。

    • 按照与语言:)关联的错误管理构造!

    推荐答案

    这似乎很奇怪,我认为要捕捉异常,并把它们变成错误codeS。为什么你认为调用者将preFER错误codeS过例外,当后者是在Java和C#的默认?

    It seems odd to me that you want to catch exceptions and turn them into error codes. Why do you think the caller would prefer error codes over exceptions when the latter is the default in both Java and C#?

    至于你的问题:


    1. 您应该只抓到,你实际上可以处理异常。只是
      捕捉异常不是在大多数情况下做正确的事。
      有少数例外(如日志记录和编组异常
      线程之间),但即使是对那些情况下,你通常应该
      重新抛出异常。

    2. 您绝对不应该有很多的try / catch语句在你的
      code。同样,这个想法是只抓到你可以处理异常。
      您可能会包括最顶层的异常处理程序把任何未处理
      异常转换成有用有所为最终用户的东西,但
      否则,你不应该试图抓住每一个例外
      每一个可能的地方。

    这篇关于在Java或C#异常管理的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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