Kazekage Gaara
Kazekage Gaara

Reputation: 15052

How to handle exception and yet complete the functionality of the method without prompting the user of the exception that has occured

Suppose there is a situation like :

statement 1;
statement 2;
statement 3;
statement 4;
statement 5;
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;

Any of these statements is suspected to raise an Exception. So I put them in a try-catch block.

try{
    statement 1;
    statement 2;
    statement 3;
    statement 4;
    statement 5;
    statement 6;
    statement 7;
    statement 8;
    statement 9;
    statement 10;
}catch(Exception e){
    e.printStackTrace();
}

Now, suppose to complete my calculations, I need to process all those 10 statements, no matter what. How do I handle the situation?

For example, say I have 10 Strings. Any of them could throw a NullPointerException when the user decides to leave the field blank(say cases like Not Applicable on forms). If I were to develop the whole project, I could first check of the null and return values accordingly for processing. But assuming I only have to develop this module to process the data.

I could put individual if checks to check which String is null, ignore it's value in the processing, and move on. But if I have n number of Strings, introducing n if statements wouldn't be tidy. Would it?

I could have individual try catch and keep processing even if an Exception occurs. But then again, wouldn't be tidy.

I could analyze the stack trace and get the line number and jump to the next line and resume the processing.(Can I? I'm not sure about it. Jumping back from a catch to a try? Not possible I believe, but still wrote this down as a possibility).

But these methods don't seem appropriate and correct.

Is there a better and a simpler way to handle such situations? I have just taken example of a String,the data to be processed could be anything, and could generate any Exception.

To repeat my question once again : Is there a way to complete the evaluation of a method or a process, no matter how many exceptions are generated in it?

The question might be a duplicate(I didn't find one though), or might not be appropriately phrased. Feel free to edit it(or suggest an edit),but keeping the main idea still focused.

Upvotes: 1

Views: 117

Answers (3)

Henrique Ordine
Henrique Ordine

Reputation: 3337

Maybe you could put the Strings/Statements inside an Array and place the Try Catch block inside a loop that loops through the Array elements.

Something like this:

for (Statement statement: statements) {
  try{
    statement;
  }catch(Exception e){
    e.printStackTrace();
  }
}

Upvotes: 3

Mahesh Velaga
Mahesh Velaga

Reputation: 21971

I second what Jon Skeet said about this being a code smell(violating single responsibility principle). To add:

If data validation could prevent these exceptions, then you could also think of writing generic validators (or based on type) and then call the validator before performing your action (only do the action if the data validation succeeds).

With this setup, considering my assumptions are correct, you could log something when validation fails and also can do all these 10 statements in a loop (as the code path would be similar)

foreach (var statement in statements)
{
       if (statement.IsDataValid()) 
       {
           statement.Execute();
       }
       else
       {
           // Log stuff or handle appropriately
       }
}

Just giving direction, as I don't have too much information on your code or data.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502106

I could put individual if checks to check which String is null, ignore it's value in the processing, and move on. But if I have n number of Strings, introducing n if statements wouldn't be tidy. Would it?

Are you doing the same thing with each string, you should extract the "check and optionally do something" behaviour into a method.

You definitely shouldn't call a method on a null reference which you know can legitimately null. NullPointerException should always indicate a bug somewhere (either you shouldn't have been given the null reference, or if that's legitimate you should check it before you do anything with it).

If your method is doing 10 separate things, each of which can fail, it may well be too big to start with. It sounds like they're independent operations (otherwise you couldn't perform operation 2 after operation 1 had failed) so put each into a separate method with a try/catch block, and call them from your top-level method.

Fundamentally this feels like a code smell though - it's very rarely a good idea to continue blithely with a series of steps in the face of an exception, and almost certainly not just by catching Exception. You should think carefully about whether this is really the design you want.

There's definitely nothing in the language to make this easier for you - precisely because it's almost always the wrong thing to do.

Upvotes: 4

Related Questions