Jools
Jools

Reputation: 155

How to continue with If-Conditions in Try-Catch

I have this line of Code

try {
        String txtText = article.getTxtText().toString();
        if (StringUtils.hasText(article.getTxtText().toString())){
            textPropertyList.add(txtText);
        }
        String txtLongText = article.getObjLongTextData().toString();
        if (StringUtils.hasText(txtLongText)){
            textPropertyList.add(txtLongText);
        }
        String txtShortText = article.getObjShortTeaserData().toString();
        if (StringUtils.hasText(txtShortText)) {
            textPropertyList.add(txtShortText);
        }
    } catch (NullPointerException e) {

    }

It is possible, that only one of the three properties are set. But if one property isnt set, I get this NullpointerException. I catch it, but then the try-Block isnt continued.

So e.g. if the article.getTxtText() method returns null, I dont get the txtLongText and txtShortText Strings either, although at least one of them has a not empty String set.

So the question is, how can I continue the try-block although there's is an Exception caught?

Thanks a lot.

Upvotes: 2

Views: 2844

Answers (7)

Andreas Dolk
Andreas Dolk

Reputation: 114817

I'd recommend a different design:

private void addProperty(Object property, Collection<String> properties) {
  if (property == null) {
    return;
  }

  String textProperty = property.toString();
  if (StringUtils.hasText()) {
    properties.add(textProperty);
  }
}

Usage:

addProperty(article.getTxtText());
// ...

Upvotes: 0

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8540

do defensive programming ,check for nulls.

if ( variable != null ){

    ...

}

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613461

If you must do this with exceptions (and I don't think you should), then you need to have 3 separate try/catch blocks:

try {
        String txtText = article.getTxtText().toString();
        if (StringUtils.hasText(article.getTxtText().toString())){
            textPropertyList.add(txtText);
        }
} catch (NullPointerException e) {}
try {
        String txtLongText = article.getObjLongTextData().toString();
        if (StringUtils.hasText(txtLongText)){
            textPropertyList.add(txtLongText);
        }
} catch (NullPointerException e) {}
try {
        String txtShortText = article.getObjShortTeaserData().toString();
        if (StringUtils.hasText(txtShortText)) {
            textPropertyList.add(txtShortText);
        }
} catch (NullPointerException e) {}

Once an exception is thrown in your code you cannot restart execution in the middle of the try block.

Having said that I would always prefer to detect the null pointer with an if test rather than relying on exception handling for this non-exceptional condition.

Upvotes: 2

aleroot
aleroot

Reputation: 72676

The simplest and better approach from my point of view would be break the try - catch block in three different try-catch block, something like the following :

try {
        String txtText = article.getTxtText().toString();
        if (StringUtils.hasText(article.getTxtText().toString())){
            textPropertyList.add(txtText);
        }

    } catch (NullPointerException e) {
      //Handle Exception
    }

try {
        String txtLongText = article.getObjLongTextData().toString();
        if (StringUtils.hasText(txtLongText)){
            textPropertyList.add(txtLongText);
        }

 } catch (NullPointerException e) {
      //Handle Exception
    }

try {
        String txtShortText = article.getObjShortTeaserData().toString();
        if (StringUtils.hasText(txtShortText)) {
            textPropertyList.add(txtShortText);
        }
     } catch (NullPointerException e) {
      //Handle Exception
    }

Upvotes: 0

FinalFrag
FinalFrag

Reputation: 136

You should either use 3 try-catch blocks or just use a null-check around every case.

if (article.getTxtText() != null) {
  // do part 1
}

if (article.getObjLongTextData() != null) {
  // do part 2
}

Upvotes: 6

Chris
Chris

Reputation: 27629

I would imagine that the correct approach to this is to have three try/catch blocks around each point of code. The whole point of a try block is that you are trying the code as a lump and if it fails anywhere you abandon it. For what you are describing you would need three try/catches around each possible point of failure.

That having been said you are probably better off testing for null rather than relying on exception handling to do that. Exception handling should be for exceptionalm unforeseen events, not for flow control in a program.

Upvotes: 3

scibuff
scibuff

Reputation: 13765

Why are you doing this in a try / catch, just use simple if

if ( txtText != null ){
    ...
}
if ( txtLongText != null ){
    ...
} 

Upvotes: -1

Related Questions