简体   繁体   English

Java方法包含另一个引发异常的方法

[英]Java method contains another method that throws exception

I have public method add(String) that calls private method inspectSequence(String) to check whether String valid or not. 我有公共方法add(String) ,该方法调用私有方法inspectSequence(String)来检查String是否有效。

This method returns array if passed String is valid, if it's invalid then method throws IllegalArgumentException 如果传递的String有效,则此方法返回array;如果无效,则方法抛出IllegalArgumentException

the code is next 代码是下一个

public void add(String sequence) throws IllegalArgumentException{
  inspectSequence(sequence);
}

private int[] inspectSequence(String sequence){
  int[] array;
  //some actions
  if(<some condition>) throw new IllegalArgumentException("description");
  return array;
}

So in cases, when invalid String were passed to add method output will be next: 因此,如果传递了无效的Stringadd方法,则输出将是下一个:

java.lang.IllegalArgumentException: description
at inspectSequence
at add

But I don't want user to know about private inspectSequence method because this is realization detail, right? 但是我不想让用户知道私有的inspectSequence方法,因为这是实现细节,对吗?

So what I can do in that case? 那我该怎么办? Is it a good idea to throw unchecked exception here? 在这里抛出未经检查的异常是一个好主意吗?

And is a good idea to throw exception inside inspectSequence method or I should return null when supplied String isn't valid and then check returned result in add method and depending on it throw or not throw exception? 最好将一个异常抛出到inspectSequence方法内,或者当提供的String null时我应该返回null ,然后在add方法中检查返回的结果,并取决于是否抛出异常?

But I don't want user to know about private inspectSequence method because this is realization detail, right? 但是我不想让用户知道私有的inspectSequence方法,因为这是实现细节,对吗?

I'd say no. 我会说不。 It's true that you don't want the user (which in that context means someone calling the code) to "know" about internal methods like inspectSequence() . 确实,您不希望用户(在此情况下,这意味着有人在调用代码)不希望“知道”诸如inspectSequence()类的内部方法。 With "know" I mean be able to call, depend upon etc. “知道”是指能够打电话,依靠等。

Knowing that the exception might be thrown and under what circumstances is something that a caller should know about, knowing where exactly it is thrown isn't necessary but wouldn't hurt. 明知可能会引发异常,什么情况下是什么,主叫方应该知道,知道那里究竟是抛出是没有必要的,但也不会受到伤害。

Of course you could catch that exception in the calling method and throw another one but that would just lose information and might make the code harder to debug/maintain since the information where the input was not accepted would be lost to the caller. 当然,你可以赶上在调用方法异常,并抛出一个又一个,但会失去公正的信息和可能使代码更难调试/因为输入不被接受,将失去到呼叫者的信息维护。

So what I can do in that case? 那我该怎么办? Is it a good idea to throw unchecked exception here? 在这里抛出未经检查的异常是一个好主意吗?

That depends on whether that exception should be handled at runtime or be fixed. 这取决于是应在运行时处理该异常还是对其进行修复。

Suppose the caller needs to know that the sequence was invalid and should handle that information appropriately, eg display some information to the enduser. 假设调用者需要知道该序列无效,并且应该适当地处理该信息,例如向最终用户显示一些信息。 In that case it would be better to throw a checked exception that describes that case. 在这种情况下,最好抛出一个描述该情况的检查异常。

On the other hand, if the input violates the contract of the method, ie the input sequence should never be invalid (or otherwise it's a programming error) then an IllegalArgumentException would be ok - situations like passing null to a method that doesn't expect null parameters. 另一方面,如果输入违反了方法的约定,即输入序列永远不应该无效(否则是编程错误),则IllegalArgumentException就可以了-诸如将null传递给不需要的方法空参数。

And is a good idea to throw exception inside inspectSequence method or I should return null when supplied String isn't valid and then check returned result in add method and depending on it throw or not throw exception? 最好将一个异常抛出到inspectSequence方法内,或者当提供的String无效时我应该返回null,然后在add方法中检查返回的结果,并取决于是否抛出异常?

I'd say no. 我会说不。 Returning null and then handling that in the calling method might be a reasonable way in some cases (eg if you have different callers that handle null differently) but your case is none of those. 在某些情况下(例如,如果您有不同的调用者以不同的方式处理null),则返回null然后在调用方法中进行处理可能是一种合理的方式,但您的情况并非如此。 It would make code more complex and thus harder to read and maintain, especially since null might have multiple meanings which you'd have to define in that case. 这将使代码更复杂,因此更难于阅读和维护,特别是因为null在这种情况下可能需要定义多个含义。

You can catch IllegalArgumentException and throw your own. 您可以捕获IllegalArgumentException并抛出自己的异常。

public void add(String sequence) throws MyCustomException {
    try {
        inspectSequence(sequence);
    } catch (IllegalArgumentException e) {
        throw new MyCustomException("more readable cause that hides internals");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM