选中和未选中的例外和设计思路 [英] Checked and Unchecked Exceptions and design thoughts

查看:104
本文介绍了选中和未选中的例外和设计思路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说这个不可变记录类型:

Lets say have this immutable record type:

public class Record
{
    public Record(int x, int y) {
        Validator.ValidateX(x);
        Validator.ValidateY(y);
        X=x;
        Y=y;
    }

    public final int X;
    public final int Y;

    public static class Validator {
        public void ValidateX(int x) { if(x < 0) { throw new UnCheckedException; } }
        public void ValidateY(int y) { if(y < 0) { throw new UnCheckedException; } }
    }
}

注意它会抛出未经检查的异常。原因是因为这是一个经常使用的对象,并且必须处理已检查的异常是不方便的。

Notice it throws an unchecked exception. The reason is because this is a object that is used quite often and it is inconvenient to have to deal with a checked exception.

但是,如果此对象位于类库中,可用于验证用户输入(或其他一些外部输入)。现在它开始听起来应该是一个CHECKED异常,因为输入不再是程序员。

However, if this object is in a class library where it may be used to validate user inputs (or some other external input). Now it's starting to sound like it should be a CHECKED exception, because the inputs are no longer up the to programmer.

想一想每个人?我应该选中还是取消选中,还是有更好的设计?

Thoughts everyone? should i make checked or unchecked, or are there better design for this?

更新:

我的困惑是来自这个场景:通常记录会像这样使用:

my confusion is coming from this scenerio: normally Record would be used like this:

Record r = new Record(1,2);
OtherObj o = new OtherObj(r);

这取决于程序员,因此未经检查的异常是可以的。

there it's up to the programmer, so unchecked exception is ok.

然而,当您从用户那里获得Record的参数时,您想要对它们进行验证吗?所以你可以打电话给

However when you get parameters for Record from a user you want to validate them right? so you might call

Record.ValidateX(inputX);
Record.ValidateY(inputY);

它可能会抛出一个已检查的异常,因为不再控制输入?

There it might throw a checked exception because inputs are no longer controlled?

对不起,我通常不会太在意这个问题(我个人认为未经检查就好了)。但这实际上是家庭作业中的一个问题,我想把它搞定大声笑。

Sorry, I normally wouldn't be too concerned with this (personally I think unchecked is fine). but this is actually a problem in a homework and I want to get it right lol.

UPDATE(2):
我开始认为我需要的是用于ValidateX抛出已检查的异常,因为它通常是涉及用户输入时使用的异常。在这种情况下,我们可以再次要求用户输入。但是,对于Record构造函数,它将抛出已检查的异常,因为构造具有无效参数的Record是API违规。新代码如下所示:

UPDATE(2): I starting to think what I need is for ValidateX to throw a checked exception because it is typically what would be used if user input is involved. In that case we could ask the user for input again. However, for the Record constructor, it will throw checked exception because constructing a Record with invalid arguements is an API violation. The new code would look like this:

public class Record
{
    public Record(int x, int y) {
        try
        {
            Validator.ValidateX(x);
            Validator.ValidateY(y);
        }catch(CheckedException xcpt) { throw new UnCheckedException(xcpt.getMessage()); }
        X=x;
        Y=y;
    }

    public final int X;
    public final int Y;

    public static class Validator {
        public void ValidateX(int x) throws CheckedException { if(x < 0) { throw new CheckedException; } }
        public void ValidateY(int y) throws CheckedException { if(y < 0) { throw new CheckedException; } }
    }
}

现在程序员可以在传递之前验证参数他们到Record类。如果没有那么它就是API违规并且抛出了未经检查的异常。

Now the programmer can validate the parameters before passing them to the Record class. If the does not then it's a API violation and an unchecked exception is thrown.

这听起来怎么样?!

推荐答案

您永远不应该使用未经检查的异常来验证用户输入。通常会检查异常,因为这样您就不会忘记处理异常。

You should never validate user input with an unchecked exception. Exceptions are normally checked because that way you can not forget to handle the exceptions.

验证api中使用的参数是完全不同的类型。这些应该是未选中的,因为否则你最终会为每个函数调用添加try / catch。每当一个方法传递一个无效的参数时,这肯定是一个编程错误。通常的方法是抛出IllegalArgumentException或NullPointerException(或任何其他适合您的需要)。
在api电话中留下已检查的激活

Validating parameters used in an api is a completely different kind. These should be unchecked, because otherwise you'll end up adding try/catch to every function call. Whenever a method is passed an invalid argument, this is surely a programming error. The normal way is to throw an IllegalArgumentException or NullPointerException (or any other that suits your needs). In api calls leave the checked excpetions for


  1. 您向api的来电者承诺的验证

  2. 预期的异常情况(请求的文件不存在,写入失败等)

除上述情况外,可恢复性也很重要用于决定已检查或未检查的异常。如果你永远无法恢复(这通常是编程错误中的情况)你可以(或应该?)取得未经检查的异常。

Besides the above, recoverability is also important for deciding for a checked or unchecked exception. If you can never recover (which is normally the case in a programming error) you can (or should?) take the unchecked exception.

最好不要写代码不符合上述指导方针。对于您而言,这意味着您必须编写一个函数,在用于验证用户输入时返回布尔值或已检查异常,并在验证函数的输入参数时返回未经检查的异常。当然,您可以并且应该使用相同的函数来验证异常,只需包装返回布尔值或检查异常的函数:

Preferably don't write code that doesn't meet the above guidelines. For you that would mean that you have to write a function that returns a boolean or checked exception when using to validate user input, and an unchecked exception when validating input parameters to your functions. Of course your can and should use the same function to validate the exceptions, just wrap the one that returns a boolean or checked exception:

public boolean validateX(int x)
{
     return x > 0;
}

private void validateParameter(int x)
{
     if (validateX(x))
     {
         throw new IllegalArgumentException("X is invalid");
     }
}

这是多一点工作,但它会给你是两全其美的。通过将validate参数功能设为私有,您可以确保不会在课堂外意外使用它。当然你也可以在构造函数中放入 if(validateX(x))... 部分。

This is a bit more work, but it will give you the best of both worlds. By making the validate parameter function private you can ensure you don't accidentally use it outside of your class. Of course you can also put the if(validateX(x)) ... part inside your constructor.

这篇关于选中和未选中的例外和设计思路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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