0

This is the sample code piece

public OperationResult beforeEverything(BDDObject savingObject) { String checkAssetRole = doCheckAssetRole(savingObject); if (checkAssetRole != null && !checkAssetRole.equals("MissingAssetRole")) { return new OperationResult(new OperationExecutionError("SIP-37006", new String[] {"Duplicate asset roles have been defined: " + checkAssetRole}, getLocalizationGate())); } ArrayList<String> warnings = new ArrayList<String>(); boolean showWarning = false; if (checkAssetRole != null && checkAssetRole.equals("MissingAssetRole")) { mLogger.debug("Warning of Asset role"); warnings.add(new String( "Asset role is missing. Do you want to save the record?")); showWarning = true; } return OperationResult.OK; } 

The thing is that doCheckAssetRole method returns null. So how can I handle it in beforeEverything() method. Is there some exception handling to be done? if so, how?

4 Answers 4

1

Put a null check and then throw a RuntimeException with message .

 if(checkAssetRole==null) throw new RuntimeException ("AssetRole value is NULL."); 

And your code look like this.

String checkAssetRole = doCheckAssetRole(savingObject); if(checkAssetRole==null) throw new RuntimeException ("AssetRole value is NULL."); if (checkAssetRole != null && !checkAssetRole.equals("MissingAssetRole")) { return new OperationResult(new OperationExecutionError("SIP-37006", new String[] {"Duplicate asset roles have been defined: " + checkAssetRole}, getLocalizationGate())); } 
Sign up to request clarification or add additional context in comments.

5 Comments

I think IllegalArgumentException is better than RuntimeException in that case (more specific).
1. you should also remove the null check in the following if-conditions too because they aren't necessary any more. 2. I think IllegalArgumentException isn't more specific - after all the error is not because of an illegal argument. 3. If you just remove the null checks for checkAssetRole altogether, you would get an NullPointerException - now that's what I call specific.
@Axel : As per the code posted by OP null checks are already there in all if block so he won't get NullPointerException.
This is a good answer. Can u pls tell me whether I have to handle the RuntimeException after throwing it? I mean, a try-catch block is necessary, right?
@amicngh: Yes, that's why I suggested removing the null checks.
1

It depends what you want is your business logic if doCheckAssetRole is null:

  • it corresponds to a valid business use case, so you have a corresponding OperationResult object to return for this case, so you would do something like

    if (checkAssetRole == null) return new NoAssetRoleOperationResult(); 

    And the calling code will handle this kind of result.

  • its never supposed to happen, except if the user made a mistake, then you throw a checked exception.

    if (checkAssetRole == null) throw new NoAssetRoleException(yourMessage); 

    and you make your method declaration throws NoAssetRoleExeption. The calling code is then responsible of forwarding this error back to the user.

  • it failed because of a environment error (a remote server is down), you throw an unchecked exception like RuntimeException, an top level code will catch it to indicate there is an environment failure.

  • it failed because of a development error (theorically this code should never return null), then you assert:

    assert checkAssetRole != null : "assetRole should not be null" 

Pick your case :)

1 Comment

In your first case, return new NoAssetRoleOperationResult(); the NoAssetRoleOperationResult() needs to be cast to the desired object type.
0

Returning null from a method is normal practice. The caller needs to check for it. In your case you can just do a check and act accordingly. If you want to throw and Exception, then you can do so.

 public OperationResult beforeEverything(BDDObject savingObject) { String checkAssetRole = doCheckAssetRole(savingObject); if (checkAssetRole == null) { // DO SOMETHING HERE. eg. throw new RuntimeException("Asset Role should not be null"); // or simply return null; } 

2 Comments

Can u pls tell me whether I have to handle the RuntimeException after throwing it? I mean, a try-catch block is necessary, right?
Runtime exceptions do not require a try/catch block. If you are building a web-app you need to decide what to do with that error. You can create your own exception class (sub-class RuntimeException) and then catch it further down and display a message to the user.
0

It depends on what you want to do when null is returned. If you want to return a value NotOK when null is returned from doCheckAssetRole, then you can use this fragment.

public OperationResult beforeEverything(BDDObject savingObject) { String checkAssetRole = doCheckAssetRole(savingObject); if (checkAssetRole == null) { return OperationResult.NotOK; // Or something else that indicates to the calling method what happened. else { if (!checkAssetRole.equals("MissingAssetRole")) { return new OperationResult(new OperationExecutionError("SIP-37006", new String[] {"Duplicate asset roles have been defined: " + checkAssetRole}, getLocalizationGate())); } ArrayList<String> warnings = new ArrayList<String>(); boolean showWarning = false; if (checkAssetRole.equals("MissingAssetRole")) { mLogger.debug("Warning of Asset role"); warnings.add(new String( "Asset role is missing. Do you want to save the record?")); showWarning = true; } return OperationResult.OK; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.