简体   繁体   English

异常执行数据流,为什么抛出异常时我的对象为null?

[英]exception execution data flow, why is my object is null when exception thrown?

I'm not sure of why this is happening, but I have a simple setup where a caller's return value is null. 我不确定为什么会这样,但是我有一个简单的设置,其中调用者的返回值为null。

I call a function which might throw registered exceptions. 我调用了一个可能引发注册异常的函数。 When it does, myXDSConsumerRequestHandler stays null. 当这样做时,myXDSConsumerRequestHandler保持为空。 The problem is that I'm able to recover from the registered events (checked the object on the callee). 问题是我可以从已注册的事件中恢复(已检查被调用者上的对象)。 So how do I call the retrieveDocuments and get my object back? 那么,如何调用retrieveDocuments并取回对象呢?

I understand the flow is broken when the exception is thrown, so should I be catching the exceptions at a higher level? 我知道抛出异常时流程会中断,因此我应该在更高级别上捕获异常吗?

This is the caller: 这是呼叫者:

try {
    myXDSConsumerRequestHandler = 
        RetrieveDocSetUtil.retrieveDocuments(NIST, multipleRetrieveMap);
} catch (VerboseIllegalArgumentException e) {

} catch (XDSException e) {

}

This is the callee: 这是被叫方:

public static RetrieveDocumentSetImpl retrieveDocuments(
        String repoURL, Map<String, String> docRepoMap) 
    throws VerboseIllegalArgumentException, XDSException {

    RetrieveDocumentSetImpl myXDSConsumerRequestHandler = 
            new RetrieveDocumentSetImpl(repoURL);
    myXDSConsumerRequestHandler.retrieveDocumentSet(docRepoMap);
    return myXDSConsumerRequestHandler;
}

Thank you! 谢谢!

Your code is executed in the following order: 您的代码按以下顺序执行:

  1. Call retrieveDocuments 调用retrieveDocuments
  2. Assign the result to myXDSConsumerRequestHandler 将结果分配给myXDSConsumerRequestHandler

If an exception is thrown during step 1, step 2 will never happen. 如果在步骤1中引发异常,则步骤2将永远不会发生。

In general, you cannot both throw an exception and return a value. 通常,您不能同时引发异常和返回值。

If retrieveDocuments() throws an exception then it never gets a chance to return something. 如果retrieveDocuments()引发异常,则它永远没有机会返回某些内容。 The statement myXDSConsumerRequestHandler = RetrieveDocSetUtil.retrieveDocuments() does not finish normally. 语句myXDSConsumerRequestHandler = RetrieveDocSetUtil.retrieveDocuments()无法正常完成。

It is not possible to both catch an exception from a method and receive the return value. 无法同时从方法中捕获异常并接收返回值。

If retrieveDocuments() has something meaningful it can return to callers even when these exceptions are encountered, then this method should be catching exceptions internally and returning an appropriate return value rather than allowing the exception to propagate up to the calling method. 如果retrieveDocuments()具有有意义的意义,即使遇到这些异常,它也可以返回给调用者,那么此方法应该在内部捕获异常并返回适当的返回值,而不是允许异常传播到调用方法。

You problem comes from having a methid doing too much. 您的问题来自于做过多的方法。 It both obtains a RetrieveDocumentSetImpl and attempts to use it. 它都获取RetrieveDocumentSetImpl并尝试使用它。

RetrieveDocumentSetImpl myXDSConsumerRequestHandler = 
        new RetrieveDocumentSetImpl(repoURL);
myXDSConsumerRequestHandler.retrieveDocumentSet(docRepoMap);
return myXDSConsumerRequestHandler;

Separate this into two methods: 将此分为两种方法:

RetrieveDocumentSetImpl myXDSConsumerRequestHandler = 
        new RetrieveDocumentSetImpl(repoURL);
return myXDSConsumerRequestHandler;

which either return a handler or fails, throwing an Exception, and 它要么返回处理程序,要么失败,并引发Exception,并且

 myXDSConsumerRequestHandler.retrieveDocumentSet(docRepoMap);

Then your caller can call the first method, grabbing the return code, and then if they so choose try various things such as cal the second, catching the indidual exceptions they throw. 然后,您的调用者可以调用第一个方法,获取返回码,然后,如果愿意,请尝试尝试各种操作(例如,调用第二个方法),以捕获它们引发的异常。

Here are my suggestions 这是我的建议

  1. Add proper handling on public retrieveDocuments(NIST, multipleRetrieveMap) method to ensure that proper parameters are passed to your method. 在公共的retrieveDocuments(NIST,multipleRetrieveMap)方法上添加适当的处理以确保将正确的参数传递给您的方法。 This will address exceptions specially related to VerboseIllegalArgumentException the parameter validation/handling should be done before you pass the paramters to the retrieveDocuments method. 这将解决与VerboseIllegalArgumentException特别相关的异常,在将参数传递给retrieveDocuments方法之前,应完成参数验证/处理。 Im afraid that if you encounter an exception you can no longer retreive your object for the reason stated by the previous posters It is not possible to both catch an exception from a method and receive the return value. 恐怕如果遇到异常,则由于先前发布者所述的原因,您将无法再获取对象。 无法同时从方法中捕获异常并接收返回值。 thats why proper validation and handling is very important. 这就是为什么正确的验证和处理非常重要的原因。
  2. After determining the cause of your exception, define/create your own exceptions. 确定异常的原因之后,定义/创建自己的异常。 This will save you a lot of time and headache in the future. 这将为您节省很多时间和将来的麻烦。

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

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